TExcelTAD
Home › Applied Cryptography: Ciphers, Hashes, Signatures, PKI & TLS › Password Storage: Salts, PBKDF2, scrypt, bcrypt and Argon2id

Password Storage: Salts, PBKDF2, scrypt, bcrypt and Argon2id

Every large breach of the last fifteen years has been measured by one question: how were the passwords stored? LinkedIn's 2012 breach stored passwords as unsalted SHA-1: 6.5 million hashes leaked that year, and when the full set of about 117 million surfaced in 2016, a large majority was reportedly cracked within days. Adobe's 2013 leak was 3DES in ECB mode (Block Modes). Breaches of sites that used bcrypt with a reasonable cost exposed far fewer passwords. Storage is a solved problem with four standard answers; this lesson shows each with current parameters and how to verify, upgrade and migrate.

What a password hash must do

  1. One-way: the stored value must not give back the password.
  2. Salted: a unique random salt per password, stored next to the hash, so equal passwords get different hashes and precomputed ("rainbow") tables are useless. An attacker must attack each password separately.
  3. Slow, and ideally memory-hard: each guess must cost real time and, for modern GPUs and ASICs, real memory, so an offline attacker gets thousands instead of billions of guesses per second (Password Entropy).
  4. Tunable: the cost parameters are stored with the hash and can be raised as hardware gets faster.

General-purpose hashes (SHA-256, SHA-3, BLAKE2) satisfy 1 only. They are designed to be fast, which is exactly wrong here.

The four standard algorithms

Algorithm Cost parameters Memory-hard OWASP minimum (Password Storage Cheat Sheet)
Argon2id (RFC 9106, winner of the 2015 Password Hashing Competition) memory mm, iterations tt, parallelism pp yes mm = 19 MiB, tt = 2, pp = 1 (equivalent options: 46 MiB/1, 12 MiB/3, 9 MiB/4, 7 MiB/5)
scrypt (RFC 7914) NN (CPU/memory), rr (block size), pp yes N=217N = 2^{17} (128 MiB), rr = 8, pp = 1 (or 2162^{16}/8/2, 2152^{15}/8/3, …)
bcrypt (1999) cost (log2 of rounds) a little (4 KiB) cost 10 at least, as high as the server allows; input limited to 72 bytes
PBKDF2 (RFC 8018, NIST SP 800-132) iterations, PRF no HMAC-SHA-256: 600,000; HMAC-SHA-512: 220,000; HMAC-SHA-1 (legacy only): 1,400,000

OWASP's order of preference is Argon2id, then scrypt, then bcrypt for legacy systems, and PBKDF2 where FIPS-140 validation is required. These numbers are from the cheat sheet as of November 2026; check it again when you deploy, because they change as hardware gets faster.

All four, with a fixed salt

The example derives each hash from the same password with a fixed lab salt so the output is stable; real code draws a fresh 16-byte salt per password with os.urandom(16) (the libraries below do it for you). In Pyodide, hashlib.pbkdf2_hmac and hashlib.scrypt don't exist, so PBKDF2 and scrypt come from cryptography and Argon2id from argon2-cffi.

python · try it yourself
import hashlib
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
from argon2.low_level import hash_secret_raw, Type

pw = b"correct horse battery staple"
salt = bytes.fromhex("6c61622d73616c742d30303031000000")     # fixed lab salt; real code: os.urandom(16) per user

print("unsalted SHA-256 :", hashlib.sha256(pw).hexdigest()[:32], "(same for every user with this password)")
print("salted SHA-256   :", hashlib.sha256(salt + pw).hexdigest()[:32], "(still billions of guesses/s)")
pb = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=600_000).derive(pw)
print("PBKDF2-SHA256 600k:", pb.hex()[:32])
sc = Scrypt(salt=salt, length=32, n=2**17, r=8, p=1).derive(pw)
print("scrypt N=2^17 r=8 :", sc.hex()[:32])
ar = hash_secret_raw(pw, salt, time_cost=2, memory_cost=19456, parallelism=1, hash_len=32, type=Type.ID)
print("Argon2id m=19MiB t=2:", ar.hex()[:32])
print("memory used: scrypt", 128 * 8 * 2**17 // 2**20, "MiB, Argon2id", 19456 // 1024, "MiB")
unsalted SHA-256 : c4bbcb1fbec99d65bf59d85c8cb62ee2 (same for every user with this password)
salted SHA-256   : 20bb670a8fcdcb4ed682e424be6a7f67 (still billions of guesses/s)
PBKDF2-SHA256 600k: dba39171526921981cb82d35242cabea
scrypt N=2^17 r=8 : 8525a1784cb5c588a39bcf0393fbc439
Argon2id m=19MiB t=2: 351fa13841a0c4fb57f0f79efb833cd7
memory used: scrypt 128 MiB, Argon2id 19 MiB

Each of the last three takes a noticeable fraction of a second in the browser; that delay, multiplied by every guess, is the defence. scrypt uses 128⋅r⋅N128 \cdot r \cdot N bytes, 128 MiB here, which is why it resists GPUs (each guess needs its own 128 MiB).

Using the high-level APIs

In an application you don't handle salts and parameters by hand. argon2-cffi's PasswordHasher and bcrypt produce a self-describing string (PHC format or modular crypt format) with algorithm, version, parameters, salt and hash, which you store in one column. The salt is random, so the example prints only the structure and the verification results.

python · try it yourself
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
import bcrypt

ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)   # OWASP minimum for Argon2id
stored = ph.hash("lab-only-password")          # a fresh salt inside: different every run
print(stored.split("
quot;)[1:4], "... length", len(stored)) print("right password:", ph.verify(stored, "lab-only-password")) try: ph.verify(stored, "lab-only-passw0rd") except VerifyMismatchError: print("wrong password: VerifyMismatchError") print("needs rehash with today's parameters:", ph.check_needs_rehash(stored)) print("needs rehash after raising t to 3:", PasswordHasher(time_cost=3, memory_cost=19456, parallelism=1).check_needs_rehash(stored)) b = bcrypt.hashpw(b"lab-only-password", bcrypt.gensalt(rounds=12)) print("bcrypt:", b[:7].decode(), "... length", len(b), " check:", bcrypt.checkpw(b"lab-only-password", b)) long1 = b"x" * 72 + b"A"; long2 = b"x" * 72 + b"B" h = bcrypt.hashpw(long1, bcrypt.gensalt(rounds=4)) try: print("bcrypt ignores bytes after 72:", bcrypt.checkpw(long2, h)) except ValueError as e: print("bcrypt refuses >72 bytes:", e)
['argon2id', 'v=19', 'm=19456,t=2,p=1'] ... length 97
right password: True
wrong password: VerifyMismatchError
needs rehash with today's parameters: False
needs rehash after raising t to 3: True
bcrypt: $2b$12$ ... length 60  check: True
bcrypt ignores bytes after 72: True
  • The stored Argon2id string starts $argon2id$v=19$m=19456,t=2,p=1$ followed by the Base64 salt and hash.
  • check_needs_rehash is how parameters are upgraded: after a successful login, if the stored hash uses old parameters, hash the password again (you have it in memory at that moment) and replace the stored value.
  • bcrypt's 72-byte limit: two passwords that share their first 72 bytes are the same password to bcrypt. The library version here silently ignores the rest; newer versions (bcrypt 5) raise an error instead. Limit passwords to 72 bytes for bcrypt, or pre-hash carefully (see the next lesson), or use Argon2id.

Migrating legacy hashes

You can't re-hash passwords you don't know. Two standard approaches:

  • Wrap now: replace each old hash holdh_{old} with Argon2id(hold)\text{Argon2id}(h_{old}) for all users at once, and mark them. At login compute Argon2id(old_hash(pw))\text{Argon2id}(\text{old\_hash}(pw)). Every account is protected immediately; this is what Facebook described for its legacy MD5 hashes ("password onion").
  • Upgrade on login: verify with the old scheme, then store a fresh Argon2id hash. Accounts that never log in keep the weak hash, so combine with wrapping or with expiry of dormant accounts.

How weak password storage is detected

  • Format: 32 hex digits (MD5), 40 (SHA-1), 64 (SHA-256) without a salt column; the same hash for many users; reversible encryption of passwords (any "decrypt password" function); $1$ (md5crypt) or DES crypt.
  • Parameters: bcrypt cost under 10, PBKDF2 under the current iteration counts, Argon2 with mm in KiB below the minimum.
  • Code: hashlib.sha256(password…), MessageDigest.getInstance("SHA-256") on passwords, custom loops "for extra security", password logging (request bodies in logs).
  • Timing: login that responds much faster for unknown user names (user enumeration); verify against a dummy hash for unknown users.

How it is fixed

  • New systems: Argon2id with at least the OWASP minimum, tuned so a login takes well under a second on your servers; or scrypt; bcrypt cost 12 or more where Argon2id isn't available.
  • Store the whole PHC string; rehash on login when parameters change.
  • Migrate legacy hashes by wrapping, then upgrade on login.
  • Add a pepper kept outside the database (KDFs versus Password Hashes, and Peppers), rate limiting and MFA.

Pitfalls

  • Choosing parameters so high that a burst of logins exhausts server memory: Argon2id with 1 GiB per login is a denial-of-service vector; size mm to your concurrency.
  • Doing the expensive hash on the client only: the server then stores the client's output, which becomes the password.
  • Comparing hashes with ==; use the library's verify/checkpw, which compare in constant time.

Practice

1. A database column contains 64-hex-digit values and no salt column. What is it probably, and what is the plan?

Unsalted SHA-256 (or another 256-bit fast hash). Wrap every value now with Argon2id (store Argon2id(h)\text{Argon2id}(h) and a migration flag), upgrade to plain Argon2id of the password at each login, force resets for dormant accounts, and treat the old values as exposed if a breach is suspected.

2. How much memory does scrypt with $N = 2^{16}$, $r = 8$ use per hash?

128×8×216=226128 \times 8 \times 2^{16} = 2^{26} bytes =64= 64 MiB.

3. Why does every user need their own salt rather than one site-wide salt?

With one salt, equal passwords still have equal hashes, and an attacker can hash each guess once and compare it with every user's hash at once. Per-user salts force a separate attack per password.

Test yourself

Which algorithm does OWASP recommend first for password storage?

  1. SHA-256 with a salt
  2. PBKDF2-HMAC-SHA1
  3. Argon2id
  4. bcrypt with cost 4

What does a per-user salt prevent?

  1. Guessing a single password
  2. Precomputed tables and attacking all users with one hash per guess
  3. Online brute force
  4. Phishing

What happens to a bcrypt password longer than 72 bytes?

  1. It is hashed in full
  2. Bytes after 72 are ignored (or rejected by newer libraries)
  3. It is truncated to 8 bytes
  4. bcrypt pads it

How are Argon2id parameters upgraded without knowing users' passwords?

  1. Decrypt and re-hash all passwords
  2. Rehash at the next successful login when check_needs_rehash is true
  3. Change the salt
  4. They can't be upgraded