Answer#
A database leak should not reveal passwords. Hashing with a deliberately slow function and a different random salt per user means two users with the same password get different stored values, and guessing is expensive. The argon2 crate implements Argon2id; the stored text is a self-describing string.
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
fn hash_password(password: &str) -> Result<String, argon2::password_hash::Error> {
let salt = SaltString::generate(&mut OsRng); // a new random salt for every password
Ok(Argon2::default().hash_password(password.as_bytes(), &salt)?.to_string())
}
fn verify_password(password: &str, stored: &str) -> bool {
match PasswordHash::new(stored) {
Ok(parsed) => Argon2::default().verify_password(password.as_bytes(), &parsed).is_ok(),
Err(_) => false, // a damaged stored value never verifies
}
}
fn main() {
let a = hash_password("correct horse").unwrap();
let b = hash_password("correct horse").unwrap();
println!("starts with $argon2id$: {}", a.starts_with("$argon2id$"));
println!("same password, two hashes equal: {}", a == b);
println!("right password verifies: {}", verify_password("correct horse", &a));
println!("wrong password verifies: {}", verify_password("wrong", &a));
println!("garbage in the database: {}", verify_password("correct horse", "not a hash"));
println!("the stored string is the algorithm, its parameters, the salt and the hash, e.g. {}...", &a[..a.match_indices('$').nth(3).unwrap().0 + 1]);
}starts with $argon2id$: true
same password, two hashes equal: false
right password verifies: true
wrong password verifies: false
garbage in the database: false
the stored string is the algorithm, its parameters, the salt and the hash, e.g. $argon2id$v=19$m=19456,t=2,p=1$...The stored string looks like $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>: algorithm, version, the memory, time and parallelism settings (the defaults of argon2 0.5.3 gave m=19456,t=2,p=1), the salt and the hash. Because the settings are inside the string, you can raise them later and still verify old hashes.
argon2 = "0.5.3"
password-hash = { version = "0.5", features = ["getrandom"] }The second line was needed to get OsRng (the operating system's random generator) through argon2::password_hash::rand_core; without that feature the import did not compile.
Common mistakes#
- Hashing with a fast general-purpose hash (SHA-256 and the like) and no salt.
- Comparing strings yourself. Use
verify_password, which does the check for you. - Hashing inside an
asynchandler on a runtime thread: the work is deliberately slow and blocks other requests. Move it to a blocking thread (see the thread onspawn_blocking). - Telling the client which of "no such user" and "wrong password" happened.
Related questions#
Which Argon2 settings should I use?#
The OWASP Password Storage Cheat Sheet lists minimum configurations for Argon2id, and m=19456 (19 MiB), t=2, p=1 is one of them (read 2026-12-05), the same values the crate's defaults gave above. Check the current page, because recommendations change.
Sources#
- OWASP Password Storage Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html (read 2026-12-05)
- argon2 crate: https://docs.rs/argon2/0.5.3/argon2/ (read 2026-12-05)