t_rust_less_lib/secrets_store/estimate/
zxcvbn.rs

1use super::PasswordEstimator;
2use crate::api::PasswordStrength;
3use zxcvbn::time_estimates::CrackTimeSeconds;
4use zxcvbn::ZxcvbnError;
5
6pub struct ZxcvbnEstimator {}
7
8impl PasswordEstimator for ZxcvbnEstimator {
9  fn estimate_strength(password: &str, user_inputs: &[&str]) -> PasswordStrength {
10    match zxcvbn::zxcvbn(password, user_inputs) {
11      Ok(entropy) => PasswordStrength {
12        entropy: (entropy.guesses() as f64).log2(),
13        crack_time: match entropy.crack_times().offline_fast_hashing_1e10_per_second() {
14          CrackTimeSeconds::Integer(i) => i as f64,
15          CrackTimeSeconds::Float(f) => f,
16        },
17        crack_time_display: format!("{}", entropy.crack_times().offline_fast_hashing_1e10_per_second()),
18        score: entropy.score(),
19      },
20      Err(ZxcvbnError::BlankPassword) | Err(ZxcvbnError::DurationOutOfRange) => PasswordStrength {
21        entropy: 0.0,
22        crack_time: 0.0,
23        crack_time_display: "Instant".to_string(),
24        score: 0,
25      },
26    }
27  }
28}