Find a course

Module 3: Hashing, Passwords and IntegrityLesson 6 of 1115 min

Password Storage and Message Authentication

See why salted, slow password hashes like Argon2 and bcrypt matter, estimate cracking times, and learn how HMAC provides message integrity with a key.

Step 1 of 13Learn

When the database leaks

Assume that one day, a website's user database will be stolen. It happens to large and small companies every year, and billions of account records have leaked over the past decade. The question is what the attacker gets.

If passwords were stored in plaintext, the attacker has every password instantly. Worse, many people reuse passwords, so the attacker now tries them on email, banking and shopping sites, an attack called credential stuffing.

The first improvement is to store only a hash of each password. At login, the site hashes what you type and compares the result. The site never needs the real password.

But a plain hash like SHA-256 is still weak for passwords, for two reasons:

  1. Precomputation: attackers build huge tables mapping common passwords to their hashes in advance, including space-saving variants called rainbow tables. Look up a hash, read off the password.
  2. Speed: SHA-256 is designed to be fast. A single modern GPU can compute billions of SHA-256 hashes per second, so attackers can guess through enormous lists of likely passwords and variations.

Also, identical passwords produce identical hashes, so an attacker sees immediately which users share a password, and cracking one cracks them all.

Proper password storage fixes both problems with two ingredients: salt and deliberate slowness.

Step 2 of 13Learn

Salt and slow hashing

A salt is a random value, typically 16 bytes, generated separately for each user when they set a password. The system stores the salt next to the hash and computes hash(salt + password).

Salt doesn't need to be secret. Its job is to make every user's hash unique. Two users with the password sunshine1 get completely different hashes. Precomputed tables become useless, because an attacker would need a separate table for every possible salt. And attackers must crack each user's hash individually.

The second ingredient is a slow password hashing function, also called a key derivation function, designed to take a meaningful amount of work per guess:

  • Argon2id, winner of the Password Hashing Competition in 2015 and the current first choice. It's memory-hard: each guess needs a configurable amount of RAM, which blunts the advantage of GPUs and custom chips.
  • scrypt, also memory-hard.
  • bcrypt, from 1999, still widely used and solid, with a tunable cost factor.
  • PBKDF2, which repeats a hash function many thousands of times; acceptable with a high iteration count and used where standards compliance requires it.

These functions have a work factor you can tune so that one hash takes, say, a quarter of a second on your server. A user logging in won't notice 250 milliseconds. An attacker trying billions of guesses will notice a slowdown factor of millions. As hardware gets faster, you increase the work factor.

Step 3 of 13Practice · Diagram

Storing a password the right way

Which step stops an attacker from cracking every user who chose the same password at once?

Tap the shape that answers the question, then press Check.

Storing a password the right wayProcess diagram: Password typed → Add a random salt → Slow hash (Argon2id) → Store salt + hash.PasswordtypedAdd a randomsaltSlow hash(Argon2id)Store salt +hash
Password typed

Never stored as is.

Add a random salt

16 random bytes, unique per user, stored openly next to the hash.

Slow hash (Argon2id)

Deliberately expensive in time and memory, so each guess costs the attacker too.

Store salt + hash

At login, repeat with the stored salt and compare results in constant time.

Step 4 of 13Try the calculator

Password cracking time estimator

Estimate the worst-case time to try every password of a given length and character set. Compare a fast hash (around 1010 guesses per second on GPUs) with a slow one (around 104).

Possible passwords
208,827,064,576
Time to try all (days)
0.0002 days
Time to try all (years)
0 years

Step 5 of 13Practice · Work it out

An 8-character password uses only lowercase letters (268 ≈ 2.09 × 1011 possibilities). An attacker can test 1010 guesses per second against a fast hash. About how many seconds does it take to try every possibility?

Step 6 of 13Note

Step 7 of 13Diagram

Password storage, from worst to best

Password storage, from worst to bestPyramid diagram: Plaintext, Fast hash (SHA-256), Salted fast hash, Salted slow hash.PlaintextFast hash (SHA-256)Salted fast hashSaltedslow hash

Tap a shape with to see more.

Plaintext

A leak exposes every password instantly.

Fast hash (SHA-256)

Billions of guesses per second on GPUs; identical passwords look identical.

Salted fast hash

Stops precomputed tables, but guessing is still very fast.

Salted slow hash

bcrypt, scrypt or Argon2id: each guess is costly. The recommended choice.

Step 8 of 13Practice · Spot the mistake

A developer describes how their new app stores passwords. Select the steps that are mistakes.

Step 9 of 13Learn

MACs: integrity with a key

A hash proves data hasn't changed, but only if the hash itself is trustworthy. Anyone can compute a SHA-256 hash, so an attacker who modifies a message can simply compute a new hash for it.

A message authentication code (MAC) solves this by mixing in a secret key. Only someone who has the key can compute a valid tag. Alice and Bob share a key; Alice sends the message plus MAC(key, message); Bob recomputes the MAC and compares. If Mallory changes even one bit, she can't produce a matching tag without the key. A MAC provides integrity and authenticity: Bob knows the message came from someone holding the key.

You might try to build a MAC as hash(key + message). With SHA-256 and other hash functions of the same family, that's insecure because of a length extension attack: knowing hash(key + message), an attacker can compute hash(key + message + extra data) without knowing the key, and so forge tags for extended messages. A real attack on Flickr's API worked this way in 2009.

The standard construction is HMAC, which hashes twice with the key mixed in at both stages, defeating length extension. HMAC-SHA256 is used everywhere: API request signing, JSON Web Tokens, cookie protection and inside TLS. AEAD modes such as AES-GCM have their own built-in MAC.

One implementation detail matters: compare tags with a constant-time comparison function. An ordinary comparison that stops at the first differing byte leaks timing information that can help an attacker guess a valid tag byte by byte.

Step 10 of 13Diagram

How a MAC check works

How a MAC check worksProcess diagram: Sender: HMAC(key, msg) → Message + tag sent → Receiver recomputes → Tags match?.Sender:HMAC(key,msg)Message +tag sentReceiverrecomputesTags match?

Tap a shape with to see more.

Sender: HMAC(key, msg)

Computes a tag with the shared secret key.

Message + tag sent

An attacker can change the message but cannot compute a new valid tag without the key.

Receiver recomputes

Uses the same key on the received message.

Tags match?

Compare in constant time. Mismatch → reject.

Step 11 of 13Compare

Hash, MAC and digital signature

AspectHashMAC (e.g., HMAC)Digital signature
Key usedNoneOne shared secret keyPrivate key to sign, public key to verify
Who can create a valid valueAnyoneAnyone holding the shared keyOnly the private key holder
Who can verifyAnyoneOnly holders of the shared keyAnyone with the public key
Integrity against attackersOnly if the hash comes from a trusted channelYesYes
Non-repudiationNoNo, both parties hold the keyYes

Step 12 of 13Practice · Choose one

An API requires each request to include a tag so the server can verify it came from a client that knows a shared secret and wasn't altered. Which construction should be used?

Step 13 of 13Practice · True or false

The salt used in password hashing must be kept secret, or the scheme provides no protection.