# How do I store passwords safely in a Rust web app with argon2?
Source: https://ruststudio.org/community/t/173/how-do-i-store-passwords-safely-in-a-rust-web-app-with-argon2
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, argon2 0.5.3, password-hash 0.5 (getrandom feature), Windows 11
Category: Help and questions | Kind: FAQ | Tags: argon2, passwords, security, web | Views: 0

## Short answer
Never store the password. Hash it with a slow, salted algorithm such as Argon2id from the argon2 crate: it makes a random salt per password and returns one string holding the settings, salt and hash. Store that string, and check logins with verify_password.

## Answer
## 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.

```rust title=src/main.rs
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]);
}
```

```text
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.

```toml title=Cargo.toml
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 `async` handler on a runtime thread: the work is deliberately slow and blocks other requests. Move it to a blocking thread (see the thread on `spawn_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)
