Answer#
You cannot convert a hash into another kind of hash: the plain password is gone. The only moment you can create the new hash is when the user types the password. So the migration happens at login: verify against the old format, then store a new Argon2id hash.
The function below handles both formats. It returns Some(new_hash) only when the password was correct and the stored value should be replaced. The old algorithm here is PBKDF2 (the same $pbkdf2-sha256$ PHC format as in the previous thread); the same pattern applies to any legacy format your verifier library understands.
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
use pbkdf2::Pbkdf2;
/// Returns Some(new_hash) when the password is right and the stored hash should be replaced.
fn login(password: &str, stored: &str) -> Result<Option<String>, ()> {
let parsed = PasswordHash::new(stored).map_err(|_| ())?;
if parsed.algorithm.as_str().starts_with("argon2id") {
// Already modern: verify only.
Argon2::default().verify_password(password.as_bytes(), &parsed).map_err(|_| ())?;
return Ok(None);
}
// Legacy hash (PBKDF2 here): verify with the old algorithm, then re-hash with Argon2id.
Pbkdf2.verify_password(password.as_bytes(), &parsed).map_err(|_| ())?;
let salt = SaltString::generate(&mut OsRng);
let new = Argon2::default().hash_password(password.as_bytes(), &salt).map_err(|_| ())?;
Ok(Some(new.to_string()))
}
fn main() {
let salt = SaltString::generate(&mut OsRng);
let legacy = Pbkdf2.hash_password(b"pw", &salt).unwrap().to_string();
println!("legacy starts: {}", &legacy[..14]);
let upgraded = login("pw", &legacy).unwrap().expect("should be upgraded");
println!("upgraded starts: {}", &upgraded[..9]);
println!("second login needs upgrade: {}", login("pw", &upgraded).unwrap().is_some());
println!("wrong password accepted: {}", login("nope", &legacy).is_ok());
}legacy starts: $pbkdf2-sha256
upgraded starts: $argon2id
second login needs upgrade: false
wrong password accepted: falseHow to do it#
- Look at the prefix of the stored string to choose a verifier.
- Verify with the old algorithm. If it fails, reject: never write anything.
- If it succeeds, hash the password with the current policy and update the row in the same request.
- After a defined period, report how many rows still use the old format. Decide then whether to force a reset for the remainder, or to wrap the remaining old hashes (hash the old hash with Argon2id) as a stop-gap.
- Keep the legacy verifier code until no legacy rows remain.
Common mistakes#
- Writing the new hash before the old password has been verified.
- Updating the row after sending the response, so a crash loses the upgrade.
- Leaving the legacy path open forever, so the weakest hash in the table decides your security.
Related questions#
Can I upgrade the hashes of users who never sign in?#
Not without their password. Wrapping the old hash inside a new Argon2id hash protects the stored value better, but the login code then has to run both steps; document it well, and retire it when you can.
How do I know when the migration is finished?#
Count the rows whose hash prefix is still the old one. When the count stops falling, decide whether to force a reset for the rest and then remove the legacy verifier.
Sources#
- OWASP Password Storage Cheat Sheet, "Upgrading Legacy Hashes": https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#upgrading-legacy-hashes
password-hashdocumentation: https://docs.rs/password-hash/0.5.0/password_hash/