1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use super::PasswordEstimator;
use crate::api::PasswordStrength;
use zxcvbn::time_estimates::CrackTimeSeconds;
use zxcvbn::ZxcvbnError;

pub struct ZxcvbnEstimator {}

impl PasswordEstimator for ZxcvbnEstimator {
  fn estimate_strength(password: &str, user_inputs: &[&str]) -> PasswordStrength {
    match zxcvbn::zxcvbn(password, user_inputs) {
      Ok(entropy) => PasswordStrength {
        entropy: (entropy.guesses() as f64).log2(),
        crack_time: match entropy.crack_times().offline_fast_hashing_1e10_per_second() {
          CrackTimeSeconds::Integer(i) => i as f64,
          CrackTimeSeconds::Float(f) => f,
        },
        crack_time_display: format!("{}", entropy.crack_times().offline_fast_hashing_1e10_per_second()),
        score: entropy.score(),
      },
      Err(ZxcvbnError::BlankPassword) => PasswordStrength {
        entropy: 0.0,
        crack_time: 0.0,
        crack_time_display: "Instant".to_string(),
        score: 0,
      },
    }
  }
}