Find a course

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

Cryptographic Hash Functions

Learn the properties of secure hash functions, why MD5 and SHA-1 are broken, how the birthday bound sets collision strength, and where hashes are used.

Step 1 of 14Learn

A fingerprint for data

Download a Linux installer and the website lists a long string like 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 next to it. That's a hash: a fixed-size fingerprint of the file. Compute the hash of your downloaded copy; if it matches, the file arrived intact.

A cryptographic hash function takes input of any size, a single letter or a 50 GB video, and produces an output of fixed size, called a digest. SHA-256, the most widely used today, always outputs 256 bits, written as 64 hexadecimal characters.

Good hash functions have several properties:

  • Deterministic: the same input always gives the same output.
  • Fast to compute for any input.
  • Avalanche effect: change one bit of input and about half the output bits change, unpredictably. The hashes of hello and Hello look completely unrelated.
  • Preimage resistance: given a hash, it's infeasible to find any input that produces it. The function is one-way.
  • Second-preimage resistance: given one input, it's infeasible to find a different input with the same hash.
  • Collision resistance: it's infeasible to find any two different inputs with the same hash.

Collisions must exist, since infinitely many inputs map to a finite number of outputs. The point is that nobody can actually find one. For SHA-256, no collision has ever been found.

Step 2 of 14Diagram

Any input, fixed-size fingerprint

Any input, fixed-size fingerprintProcess diagram: Any input → SHA-256 → 256-bit digest.Any inputSHA-256256-bit digest

Tap a shape with to see more.

Any input

A single word or a 4 GB installer.

SHA-256

A public, keyless, deterministic function.

256-bit digest

Always 64 hex characters. Change one input bit and about half the output bits flip (the avalanche effect).

Step 3 of 14Practice · Match the pairs

Match each hash property to the attack it prevents.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Preimage resistance
  2. Second-preimage resistance
  3. Collision resistance
  4. Avalanche effect

Step 4 of 14Learn

The birthday bound

How many people must be in a room before there's a 50% chance that two share a birthday? Most people guess around 180. The answer is just 23. The reason: with 23 people there are 253 different pairs, and each pair is a chance for a match.

The same math applies to hash collisions. To find a collision in an n-bit hash, an attacker doesn't need to try 2n inputs. By hashing random inputs and waiting for any two to match, they expect a collision after roughly 2^(n/2) attempts. This is the birthday bound.

So a hash's collision resistance is only about half its length in bits:

  • A 128-bit hash such as MD5 offers at most 264 collision resistance, within reach of large computing efforts even with no further weakness.
  • SHA-256 offers about 2128, far beyond reach.

This is why modern hash functions output at least 256 bits. It's also why a short checksum like a 32-bit CRC, perfectly fine for catching accidental transmission errors, is useless against attackers: collisions appear after only about 216 = 65,536 tries.

The birthday bound affects more than hashes. Random identifiers, nonces and session tokens collide sooner than intuition suggests, which is why systems generating random IDs at scale use 128 bits or more.

Step 5 of 14Practice · Chart

Chance that two people share a birthday

Tap the smallest group where a shared birthday is more likely than not.

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

Chance that two people share a birthdayLine chart with 9 categories: 5: 2.7%; 10: 11.7%; 15: 25.3%; 20: 41.1%; 23: 50.7%; 30: 70.6%; 40: 89.1%; 50: 97%; 60: 99.4%.0%20%40%60%80%100%51015202330405060People in the room50.7%
Exact values assuming 365 equally likely birthdays.
Chance that two people share a birthday
People in the roomProbability
52.7%
1011.7%
1525.3%
2041.1%
2350.7%
3070.6%
4089.1%
5097%
6099.4%

Step 6 of 14Practice · Work it out

Using the birthday bound, roughly how many random inputs must an attacker hash to expect a collision in a 64-bit hash function? Give the answer as a power of two: type only the exponent.

Step 7 of 14Learn

Broken hashes: MD5 and SHA-1

Hash functions age, and some fail.

MD5 (1992, 128-bit output) was once everywhere. Researchers found practical collisions in 2004, and by 2008 a team used MD5 collisions to forge a rogue certificate authority certificate that browsers would have trusted. In 2012, the Flame espionage malware used an MD5 collision to make itself look like a genuine Microsoft update. Today an MD5 collision can be produced on a laptop in seconds.

SHA-1 (1995, 160-bit output) was the standard for years. Theoretical weaknesses appeared in 2005; in 2017, Google and CWI Amsterdam announced SHAttered, the first public SHA-1 collision: two different PDF files with the same SHA-1 hash, produced with a very large but feasible amount of computation. Browsers had already stopped accepting SHA-1 certificates, and Git, which identified commits by SHA-1, added collision detection and support for SHA-256.

The current recommendations:

  • SHA-256 and SHA-512 from the SHA-2 family, designed by the NSA and published by NIST in 2001, remain secure.
  • SHA-3 (2015) was chosen through an open competition and uses a completely different internal design, called a sponge, as insurance in case SHA-2 is ever weakened.
  • BLAKE2 and BLAKE3 are fast, modern alternatives popular in software.

Note that a collision attack on MD5 doesn't mean MD5 is reversible. Preimage attacks remain impractical. But for anything involving signatures or trust, broken collision resistance is enough to disqualify a hash.

Step 8 of 14Timeline

How MD5 and SHA-1 fell

  1. 1992
    MD5 published

    128-bit output.

  2. 1995
    SHA-1 published

    160-bit output.

  3. 2001
    SHA-2 family published

    Includes SHA-256, still secure.

  4. 2004
    First practical MD5 collisions
  5. 2008
    Rogue CA certificate

    MD5 collisions used to forge a trusted certificate authority.

  6. 2015
    SHA-3 standardized

    A completely different design, as a backup.

  7. 2017
    SHAttered

    First public SHA-1 collision: two different PDFs with the same hash.

Step 9 of 14Practice · Sort into groups

Sort these hash functions by whether they're considered secure for new security uses.

Tap an item, then tap its group (or drag it there).

Step 10 of 14Learn

Where hashes are used

Hash functions are the quiet workhorses of security:

  • Integrity checks: verifying downloads and backups. Caution: a hash only helps if you get it from a source the attacker can't also change. If a hacked website serves both a malicious file and a matching hash, the check passes. Signed hashes solve this.
  • Digital signatures: signing a 2 GB file directly would be slow, so systems hash the file and sign the 32-byte digest instead. Collision resistance is essential here; otherwise an attacker could get you to sign a harmless document whose hash matches a harmful one.
  • Password storage: covered in the next lesson, with important caveats.
  • Commitments: publish a hash of a prediction now, reveal the prediction later; the hash proves you didn't change it.
  • Content addressing and deduplication: Git names every file version by its hash; backup tools and cloud storage spot duplicate data by comparing hashes.
  • Blockchains: each block includes the hash of the previous block, so altering history would change every later hash. Bitcoin's proof-of-work requires miners to find inputs whose SHA-256 hash starts with many zero bits.
  • Key derivation: deriving several keys from one secret, using constructions such as HKDF.

A hash is not encryption. There's no key and nothing to decrypt. If someone says data is encrypted with SHA-256, they're confused about which tool they're using.

Step 11 of 14Chart

Collision resistance is half the output size

Collision resistance is half the output sizeBar chart with 4 categories: MD5: Output bits 128 bits, Ideal collision security 64 bits; SHA-1: Output bits 160 bits, Ideal collision security 80 bits; SHA-256: Output bits 256 bits, Ideal collision security 128 bits; SHA-512: Output bits 512 bits, Ideal collision security 256 bits.0 bits200 bits400 bits600 bitsMD5128 bits64 bitsSHA-1160 bits80 bitsSHA-256256 bits128 bitsSHA-512512 bits256 bits
Ideal values from the birthday bound; MD5 and SHA-1 are broken well below them.
Collision resistance is half the output size
ItemOutput bitsIdeal collision security
MD5128 bits64 bits
SHA-1160 bits80 bits
SHA-256256 bits128 bits
SHA-512512 bits256 bits

Step 12 of 14Note

Step 13 of 14Practice · Choose one

Two files have identical SHA-256 hashes. What is the most reasonable conclusion?

Step 14 of 14Practice · True or false

Because MD5 collisions can be found easily, an attacker can also take any MD5 hash and quickly recover the original input.