Answer#
Argon2 has three cost settings: memory (m, in KiB), number of passes (t) and parallel lanes (p). The OWASP Password Storage Cheat Sheet, read for this thread, lists Argon2id with a minimum of 19 MiB of memory, 2 iterations and 1 degree of parallelism, and advises benchmarking on your own servers and tuning upward while logins stay fast enough. The argon2 crate's default parameters, as printed by the previous thread's program, are the same (m=19456,t=2,p=1).
The code below states the policy explicitly and notices a hash that is weaker than the policy, so it can be replaced the next time that user signs in (you have the plain password only at that moment).
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, SaltString},
Algorithm, Argon2, Params, Version,
};
// Our current policy: Argon2id, 19 MiB of memory, 2 passes, 1 lane.
fn current() -> Argon2<'static> {
let params = Params::new(19 * 1024, 2, 1, None).expect("valid parameters");
Argon2::new(Algorithm::Argon2id, Version::V0x13, params)
}
/// True when a stored hash was made with weaker settings than the policy above.
fn needs_rehash(stored: &str) -> bool {
let Ok(parsed) = PasswordHash::new(stored) else { return true };
let Ok(p) = Params::try_from(&parsed) else { return true };
parsed.algorithm.as_str() != "argon2id" || p.m_cost() < 19 * 1024 || p.t_cost() < 2
}
fn main() {
let salt = SaltString::generate(&mut OsRng);
let new = current().hash_password(b"pw", &salt).unwrap().to_string();
let weak = Params::new(8 * 1024, 1, 1, None).unwrap();
let old = Argon2::new(Algorithm::Argon2id, Version::V0x13, weak)
.hash_password(b"pw", &salt).unwrap().to_string();
println!("new hash starts: {}", &new[..31]);
println!("old hash starts: {}", &old[..30]);
println!("new needs rehash: {}", needs_rehash(&new));
println!("old needs rehash: {}", needs_rehash(&old));
}new hash starts: $argon2id$v=19$m=19456,t=2,p=1$
old hash starts: $argon2id$v=19$m=8192,t=1,p=1$
new needs rehash: false
old needs rehash: trueHow to do it#
- Write your policy in one function (
current()above), so it is changed in one place. - Benchmark on the real server, in a release build, with the number of simultaneous logins you expect. Pick the strongest setting that keeps a login quick and memory use safe under load.
- At each successful login, call
needs_rehash; if it is true, hash the password again with the current policy and save the new PHC string. - Review the parameters now and then; hardware improves.
Common mistakes#
- Setting very high memory without thinking about concurrency: 100 logins at the same moment need 100 times the memory.
- Rehashing without verifying first. Only replace a hash after the password has been checked.
- Comparing only the memory setting. A hash can also differ in algorithm, so the check above looks at that too.
Related questions#
Why Argon2id and not Argon2i or Argon2d?#
Argon2id is the hybrid variant that the OWASP sheet and RFC 9106 recommend for password hashing.
Can I change the salt length or output length?#
The defaults are fine for passwords. Leave them unless a specification you must follow says otherwise.
Sources#
- OWASP Password Storage Cheat Sheet (read 2026-12-05): https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
- RFC 9106, section 4 "Parameter Choice": https://www.rfc-editor.org/rfc/rfc9106#name-parameter-choice
argon2::Params: https://docs.rs/argon2/0.5.3/argon2/struct.Params.html