Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored score to type Score enum, for failure proof matching #77

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use zxcvbn::zxcvbn;

fn main() {
let estimate = zxcvbn("correcthorsebatterystaple", &[]);
println!("{}", estimate.score()); // 3
println!("{:?}", estimate.score()); // Medium
}
```

Expand Down
5 changes: 3 additions & 2 deletions src/feedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::frequency_lists::DictionaryType;
use crate::matching::patterns::*;
use crate::matching::Match;
use crate::Score;
use std::fmt;

/// A warning explains what's wrong with the password.
Expand Down Expand Up @@ -153,7 +154,7 @@ impl Feedback {
}
}

pub(crate) fn get_feedback(score: u8, sequence: &[Match]) -> Option<Feedback> {
pub(crate) fn get_feedback(score: Score, sequence: &[Match]) -> Option<Feedback> {
if sequence.is_empty() {
// default feedback
return Some(Feedback {
Expand All @@ -164,7 +165,7 @@ pub(crate) fn get_feedback(score: u8, sequence: &[Match]) -> Option<Feedback> {
],
});
}
if score >= 3 {
if score == Score::Medium || score == Score::Perfect {
return None;
}

Expand Down
35 changes: 25 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
mod scoring;
pub mod time_estimates;

/// score type: Variants are self explanatory
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Score {
/// SuperWeak: too lazy! 🙄
SuperWeak,
/// VeryWeak: a useless password ❌
VeryWeak,
/// Weak: easily crackable 👎
Weak,
/// Medium: need a bit more effort 😩
Medium,
/// Perfect: just enough, for now :)💪
Perfect,
}

#[cfg(not(target_arch = "wasm32"))]
fn time_scoped<F, R>(f: F) -> (R, Duration)
where
Expand Down Expand Up @@ -67,7 +82,7 @@

/// Contains the results of an entropy calculation
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ser", derive(Serialize))]

Check failure on line 85 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

the trait bound `Score: serde::Serialize` is not satisfied
pub struct Entropy {
/// Estimated guesses needed to crack the password
guesses: u64,
Expand All @@ -77,7 +92,7 @@
crack_times: time_estimates::CrackTimes,
/// Overall strength score from 0-4.
/// Any score less than 3 should be considered too weak.
score: u8,
score: Score,
/// Verbal feedback to help choose better passwords. Set when `score` <= 2.
feedback: Option<feedback::Feedback>,
/// The list of patterns the guess calculation was based on
Expand All @@ -104,7 +119,7 @@

/// Overall strength score from 0-4.
/// Any score less than 3 should be considered too weak.
pub fn score(&self) -> u8 {
pub fn score(&self) -> Score {
self.score
}

Expand Down Expand Up @@ -133,8 +148,8 @@
guesses: 0,
guesses_log10: f64::NEG_INFINITY,
crack_times: CrackTimes::new(0),
score: 0,

Check failure on line 151 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

mismatched types
feedback: feedback::get_feedback(0, &[]),

Check failure on line 152 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

mismatched types
sequence: Vec::default(),
calc_time: Duration::from_secs(0),
};
Expand Down Expand Up @@ -198,7 +213,7 @@
let password = "r0sebudmaelstrom11/20/91aaaa";
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.guesses_log10 as u16, 14);
assert_eq!(entropy.score, 4);
assert_eq!(entropy.score, Score::Perfect);
assert!(!entropy.sequence.is_empty());
assert!(entropy.feedback.is_none());
assert!(entropy.calc_time.as_nanos() > 0);
Expand All @@ -221,23 +236,23 @@
fn test_zxcvbn_unicode() {
let password = "𐰊𐰂𐰄𐰀𐰁";
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.score, 1);
assert_eq!(entropy.score, Score::VeryWeak);
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_zxcvbn_unicode_2() {
let password = "r0sebudmaelstrom丂/20/91aaaa";
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.score, 4);
assert_eq!(entropy.score, Score::Perfect);
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_issue_13() {
let password = "Imaginative-Say-Shoulder-Dish-0";
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.score, 4);
assert_eq!(entropy.score, Score::Perfect);
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
Expand All @@ -247,7 +262,7 @@
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.guesses, 372_010_000);
assert!((entropy.guesses_log10 - 8.57055461430783).abs() < f64::EPSILON);
assert_eq!(entropy.score, 3);
assert_eq!(entropy.score, Score::Medium);
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
Expand All @@ -257,7 +272,7 @@
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.guesses, 1_010_000);
assert!((entropy.guesses_log10 - 6.004321373782642).abs() < f64::EPSILON);
assert_eq!(entropy.score, 2);
assert_eq!(entropy.score, Score::Weak);
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
Expand All @@ -266,7 +281,7 @@
let password = "!QASW@#EDFR$%TGHY^&UJKI*(OL";
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.guesses, u64::max_value());
assert_eq!(entropy.score, 4);
assert_eq!(entropy.score, Score::Perfect);
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
Expand All @@ -275,6 +290,6 @@
let password = "08märz2010";
let entropy = zxcvbn(password, &[]);
assert_eq!(entropy.guesses, 100010000);
assert_eq!(entropy.score, 3);
assert_eq!(entropy.score, Score::Medium);
}
}
15 changes: 8 additions & 7 deletions src/time_estimates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//! # }
//! ```

use crate::Score;
use std::fmt;

/// Back-of-the-envelope crack time estimations, in seconds, based on a few scenarios.
Expand Down Expand Up @@ -129,21 +130,21 @@ impl From<CrackTimeSeconds> for std::time::Duration {
}
}

pub(crate) fn estimate_attack_times(guesses: u64) -> (CrackTimes, u8) {
pub(crate) fn estimate_attack_times(guesses: u64) -> (CrackTimes, Score) {
(CrackTimes::new(guesses), calculate_score(guesses))
}

fn calculate_score(guesses: u64) -> u8 {
fn calculate_score(guesses: u64) -> Score {
const DELTA: u64 = 5;
if guesses < 1_000 + DELTA {
0
Score::SuperWeak // previously, 0
} else if guesses < 1_000_000 + DELTA {
1
Score::VeryWeak // peviously, 1
} else if guesses < 100_000_000 + DELTA {
2
Score::Weak // previously, 2
} else if guesses < 10_000_000_000 + DELTA {
3
Score::Medium // previously, 3
} else {
4
Score::Perfect // previously, 4
}
}
Loading