mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 07:41:23 +01:00
151 lines
4.7 KiB
Rust
151 lines
4.7 KiB
Rust
pub(crate) mod error;
|
|
|
|
use argon2::{Config, Variant};
|
|
use cmp::Ordering;
|
|
use rand::prelude::*;
|
|
use ring::digest;
|
|
use ruma::{canonical_json::try_from_json_map, CanonicalJsonError, CanonicalJsonObject};
|
|
use std::{
|
|
cmp,
|
|
str::FromStr,
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
};
|
|
|
|
// Hopefully we have a better chat protocol in 530 years
|
|
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
|
|
pub(crate) fn millis_since_unix_epoch() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.expect("time is valid")
|
|
.as_millis() as u64
|
|
}
|
|
|
|
#[cfg(any(feature = "rocksdb", feature = "sqlite"))]
|
|
pub(crate) fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
|
|
let number = match old.map(TryInto::try_into) {
|
|
Some(Ok(bytes)) => {
|
|
let number = u64::from_be_bytes(bytes);
|
|
number + 1
|
|
}
|
|
_ => 1, // Start at one. since 0 should return the first event in the db
|
|
};
|
|
|
|
Some(number.to_be_bytes().to_vec())
|
|
}
|
|
|
|
pub(crate) fn generate_keypair() -> Vec<u8> {
|
|
let mut value = random_string(8).as_bytes().to_vec();
|
|
value.push(0xff);
|
|
value.extend_from_slice(
|
|
&ruma::signatures::Ed25519KeyPair::generate()
|
|
.expect("Ed25519KeyPair generation always works (?)"),
|
|
);
|
|
value
|
|
}
|
|
|
|
/// Parses the bytes into an u64.
|
|
pub(crate) fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError> {
|
|
let array: [u8; 8] = bytes.try_into()?;
|
|
Ok(u64::from_be_bytes(array))
|
|
}
|
|
|
|
/// Parses the bytes into a string.
|
|
pub(crate) fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
|
|
String::from_utf8(bytes.to_vec())
|
|
}
|
|
|
|
pub(crate) fn random_string(length: usize) -> String {
|
|
thread_rng()
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
.take(length)
|
|
.map(char::from)
|
|
.collect()
|
|
}
|
|
|
|
/// Calculate a new hash for the given password
|
|
pub(crate) fn calculate_password_hash(password: &str) -> Result<String, argon2::Error> {
|
|
let hashing_config = Config {
|
|
variant: Variant::Argon2id,
|
|
..Default::default()
|
|
};
|
|
|
|
let salt = random_string(32);
|
|
argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config)
|
|
}
|
|
|
|
#[tracing::instrument(skip(keys))]
|
|
pub(crate) fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> {
|
|
// We only hash the pdu's event ids, not the whole pdu
|
|
let bytes = keys.join(&0xff);
|
|
let hash = digest::digest(&digest::SHA256, &bytes);
|
|
hash.as_ref().to_owned()
|
|
}
|
|
|
|
pub(crate) fn common_elements<I, F>(
|
|
mut iterators: I,
|
|
check_order: F,
|
|
) -> Option<impl Iterator<Item = Vec<u8>>>
|
|
where
|
|
I: Iterator,
|
|
I::Item: Iterator<Item = Vec<u8>>,
|
|
F: Fn(&[u8], &[u8]) -> Ordering,
|
|
{
|
|
let first_iterator = iterators.next()?;
|
|
let mut other_iterators = iterators.map(Iterator::peekable).collect::<Vec<_>>();
|
|
|
|
Some(first_iterator.filter(move |target| {
|
|
other_iterators.iter_mut().all(|it| {
|
|
while let Some(element) = it.peek() {
|
|
match check_order(element, target) {
|
|
Ordering::Greater => return false, // We went too far
|
|
Ordering::Equal => return true, // Element is in both iters
|
|
Ordering::Less => {
|
|
// Keep searching
|
|
it.next();
|
|
}
|
|
}
|
|
}
|
|
false
|
|
})
|
|
}))
|
|
}
|
|
|
|
/// Fallible conversion from any value that implements `Serialize` to a `CanonicalJsonObject`.
|
|
///
|
|
/// `value` must serialize to an `serde_json::Value::Object`.
|
|
pub(crate) fn to_canonical_object<T: serde::Serialize>(
|
|
value: T,
|
|
) -> Result<CanonicalJsonObject, CanonicalJsonError> {
|
|
use serde::ser::Error;
|
|
|
|
match serde_json::to_value(value).map_err(CanonicalJsonError::SerDe)? {
|
|
serde_json::Value::Object(map) => try_from_json_map(map),
|
|
_ => Err(CanonicalJsonError::SerDe(serde_json::Error::custom(
|
|
"Value must be an object",
|
|
))),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn deserialize_from_str<
|
|
'de,
|
|
D: serde::de::Deserializer<'de>,
|
|
T: FromStr<Err = E>,
|
|
E: std::fmt::Display,
|
|
>(
|
|
deserializer: D,
|
|
) -> Result<T, D::Error> {
|
|
struct Visitor<T: FromStr<Err = E>, E>(std::marker::PhantomData<T>);
|
|
impl<T: FromStr<Err = Err>, Err: std::fmt::Display> serde::de::Visitor<'_> for Visitor<T, Err> {
|
|
type Value = T;
|
|
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(formatter, "a parsable string")
|
|
}
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: serde::de::Error,
|
|
{
|
|
v.parse().map_err(serde::de::Error::custom)
|
|
}
|
|
}
|
|
deserializer.deserialize_str(Visitor(std::marker::PhantomData))
|
|
}
|