Hashes in practice: integrity and passwords
Unit 2 of 5 in Cryptography basics.
You will be able to explain how passwords should be stored, what a salt and a work factor do, and what HMAC adds to a hash.
Lesson
Why a plain hash is not enough for passwords
A hash cannot be reversed, but it can be guessed against. An attacker with stolen hashes tries likely passwords, hashes each one and compares. OWASP explains that using slow, memory-hard algorithms makes brute-force attacks significantly more difficult, expensive and time-consuming. A fast general-purpose hash is the wrong tool because speed helps the attacker as much as you.
Salt: a different fingerprint for every user
A salt is a unique, randomly generated string added to each password as part of the hashing. Because it is unique for every user, an attacker has to crack hashes one at a time using each salt, rather than computing a hash once and comparing it against every stored hash. Salting also protects against precomputed tables of hashes.
Work factor: make each guess cost something
The work factor is the number of iterations of the hashing algorithm performed for each password. A higher work factor makes each hash more expensive to compute, which reduces the speed or raises the cost for an attacker trying passwords, at a small one-time cost for you at login. OWASP advises striking a balance between security and performance and raising the work factor over time.
Which algorithm
OWASP's summary: use Argon2id with a minimum configuration of 19 MiB of memory, an iteration count of 2 and a parallelism setting of 1. If Argon2id is not available, use scrypt. For legacy systems using bcrypt, use a work factor of 10 or more, with a 72 byte password limit. If FIPS-140 compliance is required, use PBKDF2 with a work factor of 600,000 or more. Argon2 is specified as a memory-hard function for password hashing in RFC 9106. These numbers change over time, so read the current guidance when you build something.
The rule that does not change: do not write your own. Use a maintained library for the algorithm you chose.
HMAC: a hash with a secret
A plain hash proves nothing about who made it, because anyone can compute one. HMAC is a mechanism for message authentication using cryptographic hash functions in combination with a secret shared key, so the check is only possible for those who hold the key. Use it when two systems that share a secret need to confirm a message has not been changed, for example a signed webhook.
Key points
- Store passwords with a slow, salted, memory-hard hash, not a fast hash.
- A salt makes every stored hash unique; a work factor makes each guess expensive.
- Prefer Argon2id; use a maintained library and current guidance.
- HMAC adds a shared secret so only key holders can make or check the code.
Sources
- OWASP Password Storage Cheat Sheet
- RFC 2104: HMAC: Keyed-Hashing for Message Authentication
- RFC 9106: Argon2 Memory-Hard Function for Password Hashing
Practice in the labs
Hash a password the right way, see how slowness protects it, then build the slow part yourself if you want to go deeper.
- How passwords are really stored: hashing, salting, and slow hashes: A short lab that shows salting and slow hashes on a test password.
- Store passwords securely with bcrypt and Argon2: You compare a fast hash with bcrypt and Argon2 and see the cost difference.
- Implement PBKDF2 from scratch, and measure what each iteration buys you: An optional deeper dive: you build the work-factor idea yourself and measure it.
Knowledge check
- Why is a fast hash a poor choice for storing passwords? Answer: Speed helps an attacker try guesses faster. OWASP recommends slow, memory-hard algorithms because they make brute-force attacks more difficult, expensive and time consuming.
- What does a unique salt per user achieve? Answer: An attacker must crack hashes one at a time instead of testing guesses against all of them at once. With unique salts, an attacker cannot compute one hash and compare it against every stored hash; the time grows with the number of hashes (OWASP).
- What does HMAC add to a plain hash? Answer: A shared secret key, so only key holders can produce or verify the code. RFC 2104 describes HMAC as message authentication using a hash function in combination with a secret shared key.
Back to the syllabus or see all classes.