Sub topic 15
Storing passwords: salts and slow hashes
This chapter assumes you know Python, bytes and hex notation, and the SHA-256 integrity check from the earlier chapter. It explains why a plain fast hash is the wrong tool for passwords and what to use instead.
Why plain SHA-256 fails here
A fast hash is a good property for integrity checks. For passwords it works against you.
Suppose an attacker steals your user table. They can guess passwords offline by hashing candidates and comparing the results with the stolen hashes. A GPU computes billions of SHA-256 hashes per second, so most human-chosen passwords fall quickly.
Plain hashing has a second weakness. The same password always gives the same hash. An attacker can precompute a table of common passwords and their hashes once, then look up every user in it. Two users with the same password also show identical hashes, which reveals that fact.
Salts
A salt is a random value, unique to each user, that is mixed into the hash input. You store it next to the hash. It is not a secret.
A salt gives you three things:
- Two users with the same password get different hashes.
- Precomputed tables become useless, because the attacker would need a separate table for every possible salt.
- The attacker must attack each account separately.
Generate salts with os.urandom, not with a predictable value such as the username. 16 bytes is enough.
Deliberately slow, memory-hungry hashes
A salt does not make each guess cost more. A password hashing function does that by making each hash expensive on purpose. The cost is a tunable work factor. Legitimate logins pay it once per attempt. An attacker pays it for every guess.
Common choices are PBKDF2, bcrypt, scrypt and Argon2. scrypt and Argon2 are also memory-hard, meaning each hash needs a large block of RAM. That limits how many guesses a GPU can run in parallel.
import hashlib
import hmac
import os
def hash_password(password: bytes):
salt = os.urandom(16)
derived = hashlib.scrypt(password, salt=salt, n=2**14, r=8, p=1)
return salt, derived
def verify_password(password: bytes, salt: bytes, derived: bytes) -> bool:
candidate = hashlib.scrypt(password, salt=salt, n=2**14, r=8, p=1)
return hmac.compare_digest(candidate, derived)
salt, stored = hash_password(b"correct horse")
print(verify_password(b"correct horse", salt, stored))
print(verify_password(b"wrong guess", salt, stored))The parameters control the cost:
nis the CPU and memory cost. It must be a power of two.ris the block size.pis the parallelism.- Memory use is about
128 * n * rbytes. Here that is 16 MiB per hash.
Verification does not decrypt anything, because the hash cannot be reversed. It recomputes the hash with the stored salt and compares the result. hmac.compare_digest compares in constant time, so the comparison time does not leak how many leading bytes matched.
Operational notes
Store the salt, the parameters and the hash together, so you can raise the cost later and still verify old records. Many libraries use a single encoded string for this.
Exercise
A site stores SHA-256(password) with no salt. Name two attacks this allows that a per-user salt and scrypt would prevent or slow down.
Show answer
First, precomputed lookup tables (rainbow tables) can crack every user at once, and a per-user random salt makes such tables useless. Second, an attacker can test billions of guesses per second because SHA-256 is fast, and scrypt's tunable time and memory cost makes each guess far more expensive. Identical passwords also produce identical hashes without a salt, which the salt fixes.