Removed magic values from gen_password. Made generate_password accept constants for the amount the length of the password
This commit is contained in:
@ -16,7 +16,7 @@ bitflags::bitflags! {
|
||||
/// Returns true if the generated master password is valid.
|
||||
/// It checks that it has at least one lowercase, one uppercase, one number and one punctuation char
|
||||
#[inline]
|
||||
fn check_generated_password(password: &[u8; 32]) -> bool {
|
||||
fn check_generated_password<const LENGTH: usize>(password: &[u8; LENGTH]) -> bool {
|
||||
let mut flags = PasswordFlags::empty();
|
||||
for &byte in password {
|
||||
match byte {
|
||||
@ -36,12 +36,12 @@ fn check_generated_password(password: &[u8; 32]) -> bool {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn generate_password<R>(rng: &mut R) -> ArrayString<32>
|
||||
fn generate_password<R, const LENGTH: usize>(rng: &mut R) -> ArrayString<LENGTH>
|
||||
where
|
||||
R: Rng + CryptoRng,
|
||||
{
|
||||
loop {
|
||||
let password: [u8; 32] = array::from_fn(|_| *CHARS.choose(rng).unwrap());
|
||||
let password = array::from_fn(|_| *CHARS.choose(rng).unwrap());
|
||||
if check_generated_password(&password) {
|
||||
return ArrayString::from_byte_string(&password).unwrap();
|
||||
}
|
||||
@ -50,7 +50,8 @@ where
|
||||
|
||||
/// Continuously generates the password until it passes the checks
|
||||
#[inline]
|
||||
pub fn generate_passwords() -> [ArrayString<32>; 10] {
|
||||
pub fn generate_passwords<const AMOUNT: usize, const LENGTH: usize>(
|
||||
) -> [ArrayString<LENGTH>; AMOUNT] {
|
||||
let mut rng = thread_rng();
|
||||
array::from_fn(|_| generate_password(&mut rng))
|
||||
}
|
||||
|
Reference in New Issue
Block a user