This commit is contained in:
timokoesters 2020-04-19 14:14:47 +02:00
parent 4d658b3952
commit 80ddf80f17
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
10 changed files with 632 additions and 141 deletions

View file

@ -1,9 +1,9 @@
use argon2::{Config, Variant};
use rand::prelude::*;
use std::{
convert::TryInto,
time::{SystemTime, UNIX_EPOCH},
};
use argon2::{Config, Variant};
pub fn millis_since_unix_epoch() -> u64 {
SystemTime::now()
@ -25,6 +25,13 @@ pub fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
Some(number.to_be_bytes().to_vec())
}
pub fn generate_keypair(old: Option<&[u8]>) -> Option<Vec<u8>> {
Some(
old.map(|s| s.to_vec())
.unwrap_or_else(|| ruma_signatures::Ed25519KeyPair::generate().unwrap()),
)
}
pub fn u64_from_bytes(bytes: &[u8]) -> u64 {
let array: [u8; 8] = bytes.try_into().expect("bytes are valid u64");
u64::from_be_bytes(array)
@ -49,9 +56,5 @@ pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
};
let salt = random_string(32);
argon2::hash_encoded(
password.as_bytes(),
salt.as_bytes(),
&hashing_config,
)
}
argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config)
}