FAQ - Help and questions

How do I move existing users' password hashes to Argon2id without a password reset?

#security#passwords#migrationpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Keep the old verifier, and when a user signs in successfully with a legacy hash, hash the password they just typed with Argon2id and replace the stored value. Detect the format from the PHC prefix. Users who never sign in keep the old hash until they do.

Checked with rustc 1.93.0, argon2 0.5.3, pbkdf2 0.12.2, Windows 11 (release build)

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

src/main.rsrust
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());
}
text
legacy starts: $pbkdf2-sha256
upgraded starts: $argon2id
second login needs upgrade: false
wrong password accepted: false

How to do it#

  1. Look at the prefix of the stored string to choose a verifier.
  2. Verify with the old algorithm. If it fails, reject: never write anything.
  3. If it succeeds, hash the password with the current policy and update the row in the same request.
  4. 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.
  5. 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.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet