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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#![doc = include_str!("../README.md")]
#![recursion_limit = "128"]
#![warn(missing_docs)]
#![forbid(unsafe_code)]
#[macro_use]
#[cfg(feature = "builder")]
extern crate derive_builder;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate quick_error;
#[cfg(feature = "ser")]
extern crate serde;
#[cfg(feature = "ser")]
#[macro_use]
extern crate serde_derive;
use std::time::Duration;
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
pub use crate::matching::Match;
mod adjacency_graphs;
pub mod feedback;
mod frequency_lists;
pub mod matching;
mod scoring;
pub mod time_estimates;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ser", derive(Serialize))]
pub struct Entropy {
guesses: u64,
guesses_log10: f64,
crack_times: time_estimates::CrackTimes,
score: u8,
feedback: Option<feedback::Feedback>,
sequence: Vec<Match>,
calc_time: Duration,
}
impl Entropy {
pub fn guesses(&self) -> u64 {
self.guesses
}
pub fn guesses_log10(&self) -> f64 {
self.guesses_log10
}
pub fn crack_times(&self) -> time_estimates::CrackTimes {
self.crack_times
}
pub fn score(&self) -> u8 {
self.score
}
pub fn feedback(&self) -> &Option<feedback::Feedback> {
&self.feedback
}
pub fn sequence(&self) -> &[Match] {
&self.sequence
}
pub fn calculation_time(&self) -> Duration {
self.calc_time
}
}
quick_error! {
#[derive(Debug, Clone, Copy)]
pub enum ZxcvbnError {
BlankPassword {
display("Zxcvbn cannot evaluate a blank password")
}
DurationOutOfRange {
display("Zxcvbn calculation time created a duration out of range")
}
}
}
#[cfg(target_arch = "wasm32")]
fn duration_since_epoch() -> Result<Duration, ZxcvbnError> {
match js_sys::Date::new_0().get_time() as u64 {
u64::MIN | u64::MAX => Err(ZxcvbnError::DurationOutOfRange),
millis => Ok(Duration::from_millis(millis)),
}
}
#[cfg(not(target_arch = "wasm32"))]
fn duration_since_epoch() -> Result<Duration, ZxcvbnError> {
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.map_err(|_| ZxcvbnError::DurationOutOfRange)
}
pub fn zxcvbn(password: &str, user_inputs: &[&str]) -> Result<Entropy, ZxcvbnError> {
if password.is_empty() {
return Err(ZxcvbnError::BlankPassword);
}
let start_time = duration_since_epoch()?;
let password = password.chars().take(100).collect::<String>();
let sanitized_inputs = user_inputs
.iter()
.enumerate()
.map(|(i, x)| (x.to_lowercase(), i + 1))
.collect();
let matches = matching::omnimatch(&password, &sanitized_inputs);
let result = scoring::most_guessable_match_sequence(&password, &matches, false);
let calc_time = duration_since_epoch()? - start_time;
let (crack_times, score) = time_estimates::estimate_attack_times(result.guesses);
let feedback = feedback::get_feedback(score, &result.sequence);
Ok(Entropy {
guesses: result.guesses,
guesses_log10: result.guesses_log10,
crack_times,
score,
feedback,
sequence: result.sequence,
calc_time,
})
}
#[cfg(test)]
mod tests {
use super::*;
use quickcheck::TestResult;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test;
quickcheck! {
fn test_zxcvbn_doesnt_panic(password: String, user_inputs: Vec<String>) -> TestResult {
let inputs = user_inputs.iter().map(|s| s.as_ref()).collect::<Vec<&str>>();
zxcvbn(&password, &inputs).ok();
TestResult::from_bool(true)
}
#[cfg(feature = "ser")]
fn test_zxcvbn_serialisation_doesnt_panic(password: String, user_inputs: Vec<String>) -> TestResult {
let inputs = user_inputs.iter().map(|s| s.as_ref()).collect::<Vec<&str>>();
serde_json::to_string(&zxcvbn(&password, &inputs).ok()).ok();
TestResult::from_bool(true)
}
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_zxcvbn() {
let password = "r0sebudmaelstrom11/20/91aaaa";
let entropy = zxcvbn(password, &[]).unwrap();
assert_eq!(entropy.guesses_log10 as u16, 14);
assert_eq!(entropy.score, 4);
assert!(!entropy.sequence.is_empty());
assert!(entropy.feedback.is_none());
assert!(entropy.calc_time.as_nanos() > 0);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_zxcvbn_unicode() {
let password = "𐰊𐰂𐰄𐰀𐰁";
let entropy = zxcvbn(password, &[]).unwrap();
assert_eq!(entropy.score, 1);
}
#[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, &[]).unwrap();
assert_eq!(entropy.score, 4);
}
#[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, &[]).unwrap();
assert_eq!(entropy.score, 4);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_issue_15_example_1() {
let password = "TestMeNow!";
let entropy = zxcvbn(password, &[]).unwrap();
assert_eq!(entropy.guesses, 372_010_000);
assert!((entropy.guesses_log10 - 8.57055461430783).abs() < f64::EPSILON);
assert_eq!(entropy.score, 3);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_issue_15_example_2() {
let password = "hey<123";
let entropy = zxcvbn(password, &[]).unwrap();
assert_eq!(entropy.guesses, 1_010_000);
assert!((entropy.guesses_log10 - 6.004321373782642).abs() < f64::EPSILON);
assert_eq!(entropy.score, 2);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_overflow_safety() {
let password = "!QASW@#EDFR$%TGHY^&UJKI*(OL";
let entropy = zxcvbn(password, &[]).unwrap();
assert_eq!(entropy.guesses, u64::max_value());
assert_eq!(entropy.score, 4);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_unicode_mb() {
let password = "08märz2010";
let entropy = zxcvbn(password, &[]).unwrap();
assert_eq!(entropy.guesses, 100010000);
assert_eq!(entropy.score, 3);
}
}