Now using thread_rng for the password generation

This commit is contained in:
StNicolay 2023-06-07 17:59:50 +03:00
parent c8c7ba154a
commit ab7012ce35
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D

View File

@ -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<R>(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]