FAQ - Help and questions

Is PBKDF2 acceptable for password hashing, and how do I use it in Rust?

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

Short answer

PBKDF2 with HMAC-SHA256 is acceptable when you need a standards-based option and cannot use Argon2id, such as under FIPS rules. Use the pbkdf2 crate with a random salt per password and a high iteration count, which OWASP currently lists as 600,000 for HMAC-SHA256.

Checked with rustc 1.93.0, pbkdf2 0.12.2 (feature simple), Windows 11 (release build)

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

#1

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.

Cargo.tomltoml
[dependencies]
pbkdf2 = { version = "0.12", features = ["simple"] }
src/main.rsrust
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());
}
text
$pbkdf2-sha256$i=600000,l=32$NEY
right: true
wrong: false

The 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#

  1. Use a new random salt for every password (done here with SaltString::generate).
  2. Set the iteration count explicitly; do not rely on a default that may change between versions.
  3. Store the full PHC string so the settings travel with the hash.
  4. 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.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet