Sub topic 16
Choosing a hash for the job
This chapter assumes you know the earlier material: collisions, the birthday bound, avalanche, hash flooding, SHA-256, HMAC and salted password hashing. It turns them into a decision procedure.
Start with the threat, not the algorithm
Ask two questions before you pick a function:
- Can an attacker choose or change the input?
- What does the attacker gain if two inputs collide, or if they guess an input?
If nobody hostile is involved, speed and distribution decide. If someone is, you need a property such as collision resistance, secrecy of a key, or deliberate slowness.
The options side by side
| Job | Use | Why | Avoid |
|---|---|---|---|
| Internal hash table, trusted keys | FNV-1a or a similar fast hash | Cheap, good spread | Summing character codes |
| Hash table with user-supplied keys | Seeded hash (keyed BLAKE2b, SipHash) | Attacker cannot predict buckets | Unseeded fast hashes |
| File or download integrity | SHA-256 | Collision and preimage resistance | MD5, SHA-1 |
| Message authentication | HMAC-SHA-256 | Needs a secret key | Plain `sha256(key + msg)` |
| Password storage | scrypt with a random salt | Costs time and memory per guess | A single fast SHA-256 |
Common mistakes
- Using a cryptographic hash where speed matters. SHA-256 works as a bucket function, but it costs more than a table lookup needs.
- Using a fast hash where an attacker is present. FNV-1a has no secret, so crafted keys can all land in one bucket.
- Using a fast cryptographic hash for passwords. SHA-256 is built to be fast, which also makes brute-force guessing fast.
- Comparing tags with
==. Usehmac.compare_digestso timing does not leak how many bytes matched.
One input, three jobs
The same bytes can need different treatment depending on what you do with them:
import hashlib
import hmac
import os
data = b"invoice-42"
# 1. Bucket in a table that only holds your own keys: speed matters.
h = 0x811C9DC5
for byte in data:
h = ((h ^ byte) * 0x01000193) & 0xFFFFFFFF
print("bucket:", h % 64)
# 2. Integrity: anyone may recompute this fingerprint.
print("sha256:", hashlib.sha256(data).hexdigest())
# 3. Authentication: only holders of the key can produce this tag.
key = os.urandom(32)
tag = hmac.new(key, data, hashlib.sha256).digest()
print("valid:", hmac.compare_digest(tag, hmac.new(key, data, hashlib.sha256).digest()))Passwords are a fourth job. The scrypt example from the password chapter applies there.
Quick checks before shipping
- Does the choice still hold if the input is attacker-controlled?
- Is the output wide enough? A 32-bit hash is likely to collide after tens of thousands of items, so it cannot serve as a unique identifier.
- Is the algorithm still unbroken? Replace MD5 and SHA-1 wherever collisions matter.
- Are keys and salts random and generated with
os.urandomor an equivalent?
Exercise
A web service stores a dictionary keyed by query-string parameter names sent by users, and separately stores user passwords. Which hash do you pick for each, and why?
Show answer
For the dictionary, use a seeded hash (secret per process, such as keyed BLAKE2b or SipHash), because the keys are attacker-controlled and an unseeded fast hash allows hash flooding. For passwords, use a salted, deliberately slow function such as scrypt, because a fast hash like SHA-256 lets an attacker test guesses cheaply.
Exercise
Why is hashlib.sha256(secret + message) a poor replacement for HMAC when authenticating a message?
Show answer
It is not designed as a keyed construction. Merkle-Damgård hashes such as SHA-256 allow length-extension attacks on this pattern. HMAC is built to avoid that and has a security proof. It also needs compare_digest for the comparison.