xmtp_cryptography/
rand.rs1use rand::distributions::Alphanumeric;
2use rand::distributions::DistString;
3use rand::{CryptoRng, RngCore, SeedableRng};
4use rand_chacha::ChaCha20Rng;
5
6use crate::Secret;
7
8pub fn rng() -> impl CryptoRng + RngCore {
9 ChaCha20Rng::from_entropy()
10}
11
12pub fn seeded_rng(seed: u64) -> impl CryptoRng + RngCore {
13 ChaCha20Rng::seed_from_u64(seed)
14}
15
16pub fn rand_string<const N: usize>() -> String {
17 Alphanumeric.sample_string(&mut rng(), N)
18}
19
20pub fn rand_array<const N: usize>() -> [u8; N] {
21 let mut buffer = [0u8; N];
22 rng().fill_bytes(&mut buffer);
23 buffer
24}
25
26pub fn rand_vec<const N: usize>() -> Vec<u8> {
27 rand_array::<N>().to_vec()
28}
29
30pub fn rand_secret<const N: usize>() -> Secret {
31 Secret::new(rand_vec::<N>())
32}