keypair logging adjustments

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry
2024-03-17 15:09:36 -04:00
parent a0161ed7c1
commit 7f22f0e3a6
3 changed files with 14 additions and 3 deletions
+5
View File
@@ -11,6 +11,7 @@ use argon2::{password_hash::SaltString, PasswordHasher};
use rand::prelude::*;
use ring::digest;
use ruma::{canonical_json::try_from_json_map, CanonicalJsonError, CanonicalJsonObject, OwnedUserId};
use tracing::debug;
use crate::{services, Error, Result};
@@ -30,8 +31,11 @@ pub(crate) fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
Some(number.to_be_bytes().to_vec())
}
/// Generates a new homeserver signing key. First 8 bytes are the version (a
/// random alphanumeric string), the rest are generated by Ed25519KeyPair
pub fn generate_keypair() -> Vec<u8> {
let mut value = random_string(8).as_bytes().to_vec();
debug!("Keypair version bytes: {value:?}");
value.push(0xFF);
value.extend_from_slice(
&ruma::signatures::Ed25519KeyPair::generate().expect("Ed25519KeyPair generation always works (?)"),
@@ -58,6 +62,7 @@ pub fn user_id_from_bytes(bytes: &[u8]) -> Result<OwnedUserId> {
.map_err(|_| Error::bad_database("Failed to parse user id from bytes"))
}
/// Generats a random *alphanumeric* string
pub fn random_string(length: usize) -> String {
thread_rng().sample_iter(&rand::distributions::Alphanumeric).take(length).map(char::from).collect()
}