|
@ -3,7 +3,7 @@ |
|
|
//
|
|
|
//
|
|
|
use std::num::NonZeroU32; |
|
|
use std::num::NonZeroU32; |
|
|
|
|
|
|
|
|
use data_encoding::HEXLOWER; |
|
|
use data_encoding::{Encoding, HEXLOWER}; |
|
|
use ring::{digest, hmac, pbkdf2}; |
|
|
use ring::{digest, hmac, pbkdf2}; |
|
|
|
|
|
|
|
|
static DIGEST_ALG: pbkdf2::Algorithm = pbkdf2::PBKDF2_HMAC_SHA256; |
|
|
static DIGEST_ALG: pbkdf2::Algorithm = pbkdf2::PBKDF2_HMAC_SHA256; |
|
@ -37,18 +37,21 @@ pub fn hmac_sign(key: &str, data: &str) -> String { |
|
|
// Random values
|
|
|
// Random values
|
|
|
//
|
|
|
//
|
|
|
|
|
|
|
|
|
pub fn get_random_64() -> Vec<u8> { |
|
|
/// Return an array holding `N` random bytes.
|
|
|
get_random(vec![0u8; 64]) |
|
|
pub fn get_random_bytes<const N: usize>() -> [u8; N] { |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_random(mut array: Vec<u8>) -> Vec<u8> { |
|
|
|
|
|
use ring::rand::{SecureRandom, SystemRandom}; |
|
|
use ring::rand::{SecureRandom, SystemRandom}; |
|
|
|
|
|
|
|
|
|
|
|
let mut array = [0; N]; |
|
|
SystemRandom::new().fill(&mut array).expect("Error generating random values"); |
|
|
SystemRandom::new().fill(&mut array).expect("Error generating random values"); |
|
|
|
|
|
|
|
|
array |
|
|
array |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Encode random bytes using the provided function.
|
|
|
|
|
|
pub fn encode_random_bytes<const N: usize>(e: Encoding) -> String { |
|
|
|
|
|
e.encode(&get_random_bytes::<N>()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// Generates a random string over a specified alphabet.
|
|
|
/// Generates a random string over a specified alphabet.
|
|
|
pub fn get_random_string(alphabet: &[u8], num_chars: usize) -> String { |
|
|
pub fn get_random_string(alphabet: &[u8], num_chars: usize) -> String { |
|
|
// Ref: https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
|
|
|
// Ref: https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
|
|
@ -77,18 +80,18 @@ pub fn get_random_string_alphanum(num_chars: usize) -> String { |
|
|
get_random_string(ALPHABET, num_chars) |
|
|
get_random_string(ALPHABET, num_chars) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn generate_id(num_bytes: usize) -> String { |
|
|
pub fn generate_id<const N: usize>() -> String { |
|
|
HEXLOWER.encode(&get_random(vec![0; num_bytes])) |
|
|
encode_random_bytes::<N>(HEXLOWER) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn generate_send_id() -> String { |
|
|
pub fn generate_send_id() -> String { |
|
|
// Send IDs are globally scoped, so make them longer to avoid collisions.
|
|
|
// Send IDs are globally scoped, so make them longer to avoid collisions.
|
|
|
generate_id(32) // 256 bits
|
|
|
generate_id::<32>() // 256 bits
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn generate_attachment_id() -> String { |
|
|
pub fn generate_attachment_id() -> String { |
|
|
// Attachment IDs are scoped to a cipher, so they can be smaller.
|
|
|
// Attachment IDs are scoped to a cipher, so they can be smaller.
|
|
|
generate_id(10) // 80 bits
|
|
|
generate_id::<10>() // 80 bits
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// Generates a numeric token for email-based verifications.
|
|
|
/// Generates a numeric token for email-based verifications.
|
|
|