diff --git a/cryptography/src/passwords.rs b/cryptography/src/passwords.rs index 02b3bb3..de90d41 100644 --- a/cryptography/src/passwords.rs +++ b/cryptography/src/passwords.rs @@ -1,5 +1,5 @@ use arrayvec::ArrayString; -use rand::{rngs::OsRng, seq::SliceRandom}; +use rand::{seq::SliceRandom, thread_rng, CryptoRng, Rng}; use std::array; const CHARS: &[u8] = br##"!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"##; @@ -36,9 +36,12 @@ fn check_generated_password(password: &[u8; 32]) -> bool { } #[inline] -fn generate_password() -> ArrayString<32> { +fn generate_password(rng: &mut R) -> ArrayString<32> +where + R: Rng + CryptoRng, +{ loop { - let password: [u8; 32] = array::from_fn(|_| *CHARS.choose(&mut OsRng).unwrap()); + let password: [u8; 32] = array::from_fn(|_| *CHARS.choose(rng).unwrap()); if check_generated_password(&password) { return ArrayString::from_byte_string(&password).unwrap(); } @@ -48,7 +51,8 @@ fn generate_password() -> ArrayString<32> { /// Continuously generates the password until it passes the checks #[inline] pub fn generate_passwords() -> [ArrayString<32>; 10] { - array::from_fn(|_| generate_password()) + let mut rng = thread_rng(); + array::from_fn(|_| generate_password(&mut rng)) } #[inline]