Skip to content
Snippets Groups Projects
utils.rs 1.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • timokoesters's avatar
    timokoesters committed
    use argon2::{Config, Variant};
    
    use std::{
        convert::TryInto,
        time::{SystemTime, UNIX_EPOCH},
    };
    
    pub fn millis_since_unix_epoch() -> u64 {
        SystemTime::now()
    
            .duration_since(UNIX_EPOCH)
            .unwrap()
    
            .as_millis() as u64
    
    timokoesters's avatar
    timokoesters committed
    
    
    pub fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
        let number = match old {
            Some(bytes) => {
                let array: [u8; 8] = bytes.try_into().unwrap();
                let number = u64::from_be_bytes(array);
                number + 1
            }
    
            None => 1, // Start at one. since 0 should return the first event in the db
    
    timokoesters's avatar
    timokoesters committed
    
    
        Some(number.to_be_bytes().to_vec())
    
    timokoesters's avatar
    timokoesters committed
    }
    
    
    timokoesters's avatar
    timokoesters committed
    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()),
    
    timokoesters's avatar
    timokoesters committed
        )
    }
    
    
    timokoesters's avatar
    timokoesters committed
    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)
    }
    
    
    /// Parses the bytes into a string.
    pub fn string_from_bytes(bytes: &[u8]) -> Result<String> {
        Ok(String::from_utf8(bytes.to_vec())?)
    
    timokoesters's avatar
    timokoesters committed
    }
    
    
    pub fn random_string(length: usize) -> String {
        thread_rng()
            .sample_iter(&rand::distributions::Alphanumeric)
            .take(length)
            .collect()
    }
    
    gnieto's avatar
    gnieto committed
    
    /// Calculate a new hash for the given password
    
    pub fn calculate_hash(password: &str) -> std::result::Result<String, argon2::Error> {
    
    gnieto's avatar
    gnieto committed
        let hashing_config = Config {
            variant: Variant::Argon2id,
            ..Default::default()
        };
    
        let salt = random_string(32);
    
    timokoesters's avatar
    timokoesters committed
        argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config)
    }