Blog | G5 Cyber Security

Better Password Hashing: Argon2 & PBKDF2

TL;DR

Yes! While bcrypt and scrypt are still good, Argon2 and PBKDF2 (with appropriate tuning) offer better resistance against modern attacks like GPU cracking. This guide explains why and how to switch.

1. Why Move Beyond Bcrypt & Scrypt?

Bcrypt and scrypt were groundbreaking but have weaknesses discovered over time, particularly with the rise of powerful hardware (GPUs). They are memory-hard, meaning they require a lot of RAM to crack quickly, but GPUs are getting more and more memory. Newer algorithms aim to be even *more* resistant.

2. Introducing Argon2

Argon2 is currently the recommended password hashing algorithm by many cyber security professionals. It’s designed to resist both GPU cracking and side-channel attacks. There are three main variants:

Most libraries default to Argon2id.

3. Introducing PBKDF2

Password-Based Key Derivation Function 2 (PBKDF2) isn’t new, but it’s still very strong *if* configured correctly. The key is using a high iteration count and a good salt.

4. How to Switch: Practical Steps

  1. Choose a Library: Select a library for your programming language that supports Argon2 or PBKDF2. Examples:
  2. Generate Salts: Always use a unique, randomly generated salt for each password. A good length is 16-32 bytes.
    # Python example using os.urandom
    import os
    salt = os.urandom(16)
  3. Hash the Password: Use your chosen library to hash the password with the salt.
    # Python Argon2 example (argon2-cffi)
    from argon2 import PasswordHasher
    ph = PasswordHasher()
    hash = ph.hash("your_password", salt=salt)
    # Python PBKDF2 example
    import hashlib
    salt = os.urandom(16)
    key = hashlib.pbkdf2_hmac('sha256', 'your_password'.encode(), salt, 100000) # High iteration count!
  4. Store the Hash & Salt: Store both the generated hash and the salt in your database. *Never* store passwords in plain text.
  5. Verification: When a user logs in, retrieve the stored hash and salt for that user. Re-hash the entered password using the same salt and compare the resulting hash with the stored hash.
    # Python Argon2 verification example
    ph = PasswordHasher()
    try:
      ph.verify(hash, "user_entered_password")
    except Exception as e:
      print("Password incorrect!")
  6. Iteration Count/Memory Settings: For PBKDF2, increase the iteration count until hashing takes around 0.5-1 second on your server. For Argon2, adjust the time cost (t), memory cost (m), and parallelism (p) parameters to achieve a similar hashing time.

5. Important Considerations

Exit mobile version