Answer#
Argon2id is the first choice for new systems because it is memory-hard, which makes large-scale guessing on special hardware more expensive. PBKDF2 only costs time, but it is widely standardised and is the OWASP recommendation when FIPS-140 compliance is required. If you use it, the iteration count is what protects you, so it must be high.
The OWASP Password Storage Cheat Sheet, read for this thread, lists 600,000 iterations for PBKDF2-HMAC-SHA256. Treat that as a minimum to revisit regularly.
[dependencies]
pbkdf2 = { version = "0.12", features = ["simple"] }use pbkdf2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Params, Pbkdf2,
};
fn main() {
// PBKDF2 with HMAC-SHA256 (the default of this crate) and an explicit work factor.
let params = Params { rounds: 600_000, output_length: 32 };
let salt = SaltString::generate(&mut OsRng);
let phc = Pbkdf2
.hash_password_customized(b"pw", None, None, params, &salt)
.unwrap()
.to_string();
println!("{}", &phc[..phc.find("$").map_or(0, |_| 32)]);
let parsed = PasswordHash::new(&phc).unwrap();
println!("right: {}", Pbkdf2.verify_password(b"pw", &parsed).is_ok());
println!("wrong: {}", Pbkdf2.verify_password(b"px", &parsed).is_ok());
}$pbkdf2-sha256$i=600000,l=32$NEY
right: true
wrong: falseThe program prints the beginning of the PHC string. The last three characters shown belong to the random salt, so they change on every run; the algorithm and the settings (i=600000,l=32) in front of them do not.
How to do it#
- Use a new random salt for every password (done here with
SaltString::generate). - Set the iteration count explicitly; do not rely on a default that may change between versions.
- Store the full PHC string so the settings travel with the hash.
- Verify with the crate's
verify_password, which compares in constant time.
Common mistakes#
- Using a low iteration count copied from an old tutorial. Counts that were reasonable years ago are too low now.
- Using PBKDF2 with a single fast hash call instead of an iterated construction.
- Treating a hash from one algorithm as verifiable by another. See the next thread for moving users from one to the other.
Related questions#
PBKDF2, bcrypt, scrypt or Argon2id?#
Prefer Argon2id. Use scrypt or bcrypt where the OWASP sheet allows it and Argon2id is unavailable, and PBKDF2 where FIPS compliance is required. All of them are better than a plain hash, and none of them replaces a unique salt.
How many iterations should I use next year?#
Check the current OWASP table when you review your settings; the recommended number has changed over time and will again.
Sources#
- OWASP Password Storage Cheat Sheet (read 2026-12-05): https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
pbkdf2documentation: https://docs.rs/pbkdf2/0.12.2/pbkdf2/- RFC 8018, "PKCS #5: Password-Based Cryptography Specification Version 2.1": https://www.rfc-editor.org/rfc/rfc8018