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
//! Generic password hashing + strength checking.
//!
//! For the tenancy-integrated user-password helpers, see
//! [`crate::tenancy::password`]. This module is the lower-level standalone
//! version — argon2id hashing + a minimal strength heuristic that doesn't
//! require importing tenancy types.
//!
//! ## Quick start
//!
//! ```ignore
//! use rustango::passwords::{hash, verify, strength_score, StrengthIssue};
//!
//! // Signup:
//! let issues = strength_score(&new_password);
//! if !issues.is_empty() {
//! return Err(format!("password too weak: {:?}", issues));
//! }
//! let hashed = hash(&new_password)?;
//! // Store `hashed` in user row.
//!
//! // Login:
//! let user = users::find_by_email(&email).await?;
//! if !verify(&attempted, &user.password_hash)? {
//! return Err("bad credentials");
//! }
//! ```
#[derive(Debug, thiserror::Error)]
pub enum PasswordError {
#[error("hashing failed: {0}")]
Hash(String),
#[error("verification error: {0}")]
Verify(String),
}
/// Hash a password with argon2id. Returns the standard PHC string format.
///
/// # Errors
/// [`PasswordError::Hash`] on argon2 failures.
pub fn hash(password: &str) -> Result<String, PasswordError> {
use argon2::password_hash::{rand_core::OsRng, PasswordHasher, SaltString};
use argon2::Argon2;
let salt = SaltString::generate(&mut OsRng);
Argon2::default()
.hash_password(password.as_bytes(), &salt)
.map(|h| h.to_string())
.map_err(|e| PasswordError::Hash(e.to_string()))
}
/// Verify a password against an argon2 PHC hash.
///
/// # Errors
/// [`PasswordError::Verify`] when `stored_hash` isn't a valid PHC string.
pub fn verify(password: &str, stored_hash: &str) -> Result<bool, PasswordError> {
use argon2::password_hash::{PasswordHash, PasswordVerifier};
use argon2::Argon2;
let parsed =
PasswordHash::new(stored_hash).map_err(|e| PasswordError::Verify(e.to_string()))?;
Ok(Argon2::default()
.verify_password(password.as_bytes(), &parsed)
.is_ok())
}
/// A valid Argon2id PHC hash of a fixed throwaway password, computed
/// once on first use (same `Argon2::default()` cost as a real stored
/// hash). Backs [`verify_dummy`].
fn dummy_hash() -> &'static str {
use std::sync::OnceLock;
static DUMMY: OnceLock<String> = OnceLock::new();
DUMMY
.get_or_init(|| {
hash("rustango-timing-equalization-dummy")
.expect("argon2id hashing of a fixed dummy input cannot fail")
})
.as_str()
}
/// Spend a password-verification's worth of work and discard the
/// result. Call this on the **user-not-found** (and inactive) branch of
/// a login flow so a request takes roughly the same time whether or not
/// the username exists. Without it, an unknown user returns before the
/// expensive Argon2 verify while a real user pays for it — a timing
/// side-channel that lets an attacker enumerate valid accounts
/// (audit H1).
pub fn verify_dummy(password: &str) {
let _ = verify(password, dummy_hash());
}
// ------------------------------------------------------------------ Strength check
/// One thing wrong with a candidate password.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StrengthIssue {
/// Shorter than the recommended 12 characters.
TooShort,
/// Contains only letters (no digits or symbols).
NoDigitsOrSymbols,
/// Uses only lowercase letters (no uppercase, digits, or symbols).
NoVariety,
/// Matches a list of well-known weak passwords.
KnownWeak,
}
/// Score a candidate password. Returns an empty `Vec` when strong enough.
///
/// Heuristics (intentionally simple — encourage users without being a
/// hard policy gate; pair with HIBP / pwned-passwords for serious deployments):
/// - Length < 12 → [`StrengthIssue::TooShort`]
/// - All-letter (no digit/symbol) → [`StrengthIssue::NoDigitsOrSymbols`]
/// - All-lowercase letters → [`StrengthIssue::NoVariety`]
/// - In the small built-in weak-password list → [`StrengthIssue::KnownWeak`]
#[must_use]
pub fn strength_score(password: &str) -> Vec<StrengthIssue> {
let mut issues = Vec::new();
if password.chars().count() < 12 {
issues.push(StrengthIssue::TooShort);
}
let has_digit = password.chars().any(|c| c.is_ascii_digit());
let has_symbol = password
.chars()
.any(|c| !c.is_alphanumeric() && !c.is_whitespace());
let has_upper = password.chars().any(|c| c.is_ascii_uppercase());
let has_lower = password.chars().any(|c| c.is_ascii_lowercase());
if !has_digit && !has_symbol {
issues.push(StrengthIssue::NoDigitsOrSymbols);
}
if !has_digit && !has_symbol && !has_upper && has_lower {
issues.push(StrengthIssue::NoVariety);
}
let lower = password.to_ascii_lowercase();
if KNOWN_WEAK.iter().any(|&w| w == lower) {
issues.push(StrengthIssue::KnownWeak);
}
issues
}
/// Top weak passwords from public breach lists. Intentionally tiny —
/// real apps should pair with HIBP's pwned-passwords API.
const KNOWN_WEAK: &[&str] = &[
"password",
"password1",
"password123",
"12345678",
"123456789",
"qwerty",
"qwerty123",
"letmein",
"admin",
"admin123",
"welcome",
"welcome1",
"iloveyou",
"monkey",
"abc123",
"111111",
"000000",
"passw0rd",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hash_and_verify_match() {
let h = hash("CorrectHorseBatteryStaple!42").unwrap();
assert!(verify("CorrectHorseBatteryStaple!42", &h).unwrap());
}
#[test]
fn verify_rejects_wrong_password() {
let h = hash("real-password").unwrap();
assert!(!verify("wrong-password", &h).unwrap());
}
#[test]
fn verify_invalid_hash_errors() {
let r = verify("anything", "not-a-valid-hash");
assert!(r.is_err());
}
#[test]
fn dummy_hash_is_valid_and_verify_dummy_does_real_work() {
// Audit H1 — the dummy hash must be a valid PHC string, else
// verify() would early-return Err and skip the argon2 cost,
// defeating the timing equalization. A real verify against it
// rejects arbitrary input (random salt over a fixed secret).
assert!(!verify("whatever-an-attacker-types", dummy_hash()).unwrap());
// Smoke: the public entry point never panics.
verify_dummy("whatever-an-attacker-types");
}
#[test]
fn strong_password_has_no_issues() {
let issues = strength_score("Tr0ub4dor&3-CorrectBattery");
assert!(issues.is_empty(), "got issues: {:?}", issues);
}
#[test]
fn short_password_flagged() {
let issues = strength_score("aB3!");
assert!(issues.contains(&StrengthIssue::TooShort));
}
#[test]
fn all_letter_password_flagged() {
let issues = strength_score("abcdefghijklmnop");
assert!(issues.contains(&StrengthIssue::NoDigitsOrSymbols));
assert!(issues.contains(&StrengthIssue::NoVariety));
}
#[test]
fn mixed_case_no_digits_only_flags_no_digits() {
let issues = strength_score("ABCDEFGHIJKLMnop");
assert!(issues.contains(&StrengthIssue::NoDigitsOrSymbols));
assert!(!issues.contains(&StrengthIssue::NoVariety));
}
#[test]
fn known_weak_password_flagged() {
let issues = strength_score("password123");
assert!(issues.contains(&StrengthIssue::KnownWeak));
}
#[test]
fn known_weak_check_is_case_insensitive() {
let issues = strength_score("PASSWORD123");
assert!(issues.contains(&StrengthIssue::KnownWeak));
}
#[test]
fn long_password_with_digit_passes_length_and_variety_check() {
let issues = strength_score("ThisIsLongEnough1");
assert!(!issues.contains(&StrengthIssue::TooShort));
assert!(!issues.contains(&StrengthIssue::NoDigitsOrSymbols));
}
}