Crypto Lecture 1 · Hash Functions: Properties and the Birthday Bound

The first of eight lessons rebuilt from the Security I cryptography lecture. It starts from three everyday uses of a fingerprint, shows with the pigeonhole principle why every hash has collisions, defines one-wayness, second-preimage resistance and collision resistance as attacker games with their generic costs, derives the birthday bound step by step to explain why an n-bit digest gives only n/2 bits of collision security, proves how the properties relate (including a first reduction), and ends by matching password storage, download checks and sealed bids to the properties they rely on.

Subject: Computer Security · 72 slides · code lesson

Open the interactive version of this deck

What this lesson covers

The lesson, slide by slide

1. Hash Functions: Properties and the Birthday Bound

Title

Computer Security — Crypto Lecture 1

A fingerprint for data: what it promises, what it costs to fake, and which promise each use relies on

2. What you will be able to do

Objectives

This is the first of eight lessons rebuilt from one long cryptography lecture. The lecture's theme runs through all of them: never trust data an adversary produced; check it instead. Hash functions are the first checking tool, and nearly everything later (MACs, signatures, certificates) is built on top of them.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-title

3. Where the eight lessons go

Concept

The original lecture covers five topics in one sitting. Here they are split into eight lessons so each one has room for practice. Every primitive gets the same treatment: what it guarantees, what it does not, and a real system that got that difference wrong.

lessontopicthe question it answers
1 (this one)Hash functionswhat does a fingerprint promise, and how hard is it to fake?
2Inside MD5 and SHA-1how does a fixed-size box hash a file of any size, and how did MD5 and SHA-1 fall?
3MACs and HMAChow do we get integrity and authentication over a hostile network?
4One-time pad and DESwhat is perfect secrecy, and how do block ciphers shrink the key?
5AESwhat happens inside the cipher everyone uses?
6Modes of operationhow do we encrypt more than one block, safely?
7CPA security, encryption plus MACwhat does secure encryption mean, and how do we combine it with a MAC?
8Public-key cryptographyhow do RSA, signatures and Diffie-Hellman work without a shared secret?

The recurring lesson: each primitive solves exactly one problem, and most famous breaks come from assuming it also solved a second one.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-roadmap

4. Part 1 — A fingerprint for data

Section

Any file in, a short fixed-size number out, and nobody can fake it

5. The plan for this lesson

Concept

A cryptographic hash function is a fingerprint for data. We build up to it in six steps, and each step answers a question the previous one raised.

  1. Why anyone wants a fingerprint of a file: three everyday uses, so the rest has a point
  2. What a hash function is, on one real example, and the fact that makes it interesting: inputs are unlimited, outputs are not
  3. The three things we need from it, each defined by the attack it stops
  4. Why finding a collision is much cheaper than it looks: the birthday paradox, computed step by step
  5. How the three properties relate to each other, with a first proof
  6. Where hashes are used, and which property each use needs

How real hash functions are built, and how MD5 and SHA-1 broke, is the next lesson. Everything here treats the hash as a black box with a fixed-size output.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-div-hash

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-plan

6. Why would anyone want a fingerprint of a file?

Concept

Three situations, before any definition. In each one, a short value stands in for something big or secret.

Figure (svg): Three cards: checking a download by comparing 64 hex characters, storing a password as a digest, and sealing a bid that is revealed later

usethe short value stands in forwhy that helps
check a downloada 6 GB installercomparing 64 characters is instant; comparing 6 GB is hopeless
store a passwordthe password itselfthe site recognises you, but a thief who steals the database gets digests
seal a bida bid you will reveal laternobody can read it now, and you cannot change it afterwards

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-why

7. What would go wrong in each use?

Socratic

Each use quietly assumes the fingerprint cannot be faked. Before any formal definition, find the weak point yourself.

Discussion prompt

For each of the three uses, what would go wrong if (a) two different inputs could be made to share a fingerprint, or (b) you could work backwards from a fingerprint to an input?

Hint: Think about what a thief holding a stolen digest wants, and what a cheating bidder wants.

Answer:

Working backwards breaks password storage: a stolen digest would hand the thief a password that works. Two inputs sharing a fingerprint breaks the download check (malware with the installer's digest) and the sealed bid (two bids with one seal, opened as whichever suits you).

Those two failure modes, in their variations, become the three security properties later in this lesson. The properties are not arbitrary definitions; they are exactly the promises these uses need.

8. What a hash function is, on one real example

Concept

A hash function H takes input of any size and outputs a fixed-size number, called the digest. We write H(x) for the digest of x. SHA-256 is the one in everyday use; run these yourself.

$ echo -n "hello" | sha256sum
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
$ echo -n "hellp" | sha256sum
fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7
$ sha256sum ubuntu-24.04.iso       # 6 GB in: still 64 hex digits out
observationwhat it means
fixed size64 hex digits every time; a hex digit is 4 bits, so 64 x 4 = 256 bits
deterministichello gives the same digest on any machine, any day, so digests can be compared
avalanchehello and hellp differ in one letter, yet the digests look unrelated
no keyanyone can compute it; the function is public, so security must come from somewhere else

The -n stops echo adding a newline. Without it you hash six bytes, not five, and get a different digest; mismatches between two systems are very often exactly this.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-what

9. How much do the two digests share?

Prediction

hello and hellp differ in one letter. Their SHA-256 digests are each 64 hex digits long.

Predict first

Line the two digests up position by position. How many of the 64 positions hold the same hex digit?

  • About 60: one letter changes only a few digits
  • About 32: half of them
  • About 4: no more than chance
  • Exactly 0: a good hash guarantees every digit changes

Correct: About 4: no more than chance

Why: For these two inputs exactly 1 of the 64 positions matches. Two random hex digits agree with chance 1 in 16, so chance alone predicts about 4. A good hash makes a tiny input change look like a completely fresh random output, which is the avalanche effect. It does not guarantee every digit changes; that would itself be a detectable pattern.

10. The inputs have no limit. The outputs do.

Concept

This is the fact everything else rests on. Count both sides.

Figure (svg): Inputs of every size, from an empty file to a 6 GB installer and beyond, all pass through H into a fixed 256-bit output space

digest lengthnumber of possible digests
1 bit2
2 bits4 (00, 01, 10, 11)
3 bits8
256 bits2 x 2 x ... x 2, 256 times = 2^256

2 to the 256 is huge, but finite. The number of possible inputs is not: there is no largest file. Infinitely many inputs are squeezed into finitely many outputs, so something has to give.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-inout

11. A toy hash with a 3-bit digest

Concept

Make it concrete with a hash that outputs only 3 bits. There are 8 possible digests, 000 to 111. Hash nine files.

Figure (svg): Eight files map to the eight digests 000 to 111; a ninth file must land on a digest already taken

collision — two different inputs with the same digest

It does not matter how clever the function is. The first eight files might all get different digests, but the ninth has nowhere new to go. Nine pigeons, eight holes: one hole holds two pigeons. This is the pigeonhole principle, and it is why the word collision exists. A 3-bit digest guarantees a collision within 9 files, so it is hopeless as a fingerprint.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-toy

12. Your turn: find the collision in a toy hash

Fill the middle

A toy 3-bit hash: add the letter positions (a = 1, b = 2, ... z = 26) and keep the remainder mod 8. For example dog = 4 + 15 + 7 = 26, and 26 mod 8 = 2. Hash four words, then spot the collision.

Fill in the blanks

cat: 3 + 1 + 20 = 24, mod 8 = 0
sun: 19 + 21 + 14 = 54, mod 8 = 6
map: 13 + 1 + 16 = 30, mod 8 = 6
the two words that collide: sun and map

Why: cat = 24, and 24 mod 8 = 0. sun = 54 = 6 x 8 + 6, so 6. map = 30 = 3 x 8 + 6, so 6. sun and map collide. With only 8 outputs, collisions show up after a handful of words; real hashes avoid this only by having an astronomically large number of outputs.

13. Now scale it up: 256 bits

Concept

Run the same argument with SHA-256. Infinitely many possible inputs, 2 to the 256 possible digests. Collisions exist, by the pigeonhole principle. Cryptography never claims otherwise.

quantitysize
possible SHA-256 digests2^256, about 1.16 x 10^77
atoms in the visible universeabout 10^80
possible inputsinfinitely many

So the real question is never whether a collision exists. It is whether anyone can find one. From here on, every security property is a statement about what an attacker can find in practice, with a realistic amount of computing.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-pigeon

14. How big is 2 to the 256?

Estimation

Use 2 to the 10 = 1024, which is about 10 to the 3.

Predict first

Roughly how many decimal digits does 2 to the 256 have?

  • About 26
  • About 77
  • About 256
  • About 1,000

Correct: About 77

Why: 256 bits is 25.6 groups of 10 bits, and each group multiplies by 1024, about 1000. So 2 to the 256 is about 1000 multiplied by itself 25.6 times, which is 10 to the 77: roughly 78 digits. The exact value is about 1.16 x 10 to the 77, a few orders of magnitude below the number of atoms in the visible universe. Exists versus findable is the whole subject of this lesson.

15. Part 2 — The three properties

Section

Each one is the promise that one attack does not work

16. Three ways a fingerprint could be faked

Socratic

Go back to the three uses. Do not use any formal names yet.

Discussion prompt

For each use, describe precisely what the attacker is handed and what they must produce to cheat: the password thief, the attacker who wants to replace a download, and the cheating bidder.

Hint: What is fixed in advance for each attacker, and what can they choose?

Answer:

Password thief: handed a digest y, must find any input that hashes to y. Download attacker: handed the real installer, so its digest is fixed, and must find a different file with the same digest. Cheating bidder: handed nothing, gets to write both bids, and needs any two inputs that share a digest.

Three attacks, three properties. A security property is simply the promise that one of these attacks does not work. Notice the bidder gets the most freedom, choosing both inputs. That freedom turns out to make their attack the cheapest.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-attacks

17. Which cheat breaks which use?

Matching

Match each use to the cheat that would ruin it.

Match the pairs

  • u1. storing passwords as digests
  • u2. checking a download against a published digest
  • u3. sealing a bid as a digest
  • c1. work backwards from a digest to some input
  • c2. find a second file matching a fixed file's digest
  • c3. prepare two inputs with one digest in advance

Why: Each use has one natural cheat, and each cheat is one property: one-wayness, second-preimage resistance and collision resistance. The sealed bid will turn out to need more than one, which we come back to at the end.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-attacks

18. The three properties, top down

Concept

Each property is defined by what the attacker is given and what they must find. n is the digest length in bits: 256 for SHA-256, 160 for SHA-1, 128 for MD5. A try is one hash computation.

propertyattacker is givenattacker must findbest attack if H is good
1. one-waya digest yany x with H(x) = yabout 2^n tries
2. second-preimage resistantan input xx' different from x with H(x') = H(x)about 2^n tries
3. collision resistantnothingany x and x', different, with H(x) = H(x')only about 2^(n/2) tries

The odd one out is the third. The attacker gets nothing, yet it is the cheapest to break, because they choose both inputs. The next slides take one property at a time, each with a real attack it stops, and then the birthday paradox explains the square root.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-props

19. The cost pattern to remember

Pattern

For a well-designed hash with an n-bit digest, the best known attacks are generic: they treat the hash as a random function and just try inputs.

attackgeneric costSHA-256 (n = 256)SHA-1 (n = 160)MD5 (n = 128)
invert a digest2^n2^2562^1602^128
second preimage2^n2^2562^1602^128
any collision2^(n/2)2^1282^802^64

A hash counts as broken when someone beats these generic costs. MD5 and SHA-1 are broken for collisions; nobody has a practical way to invert either of them.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-props

20. Property 1: one-way (you cannot go back)

Concept

one-way (preimage resistant) — given a digest y, it is hard to find any x with H(x) = y; nothing beats trying inputs one by one

Figure (svg): Forward: hunter2 goes through H to a digest instantly. Backward: no formula leads from the digest to an input, so the only move is to guess, hash and compare

Note the word any. A thief holding your password digest does not need your real password; any input that hashes to the same value logs them in. One-way does not say the attacker can never succeed. It says the only route is guessing, so security becomes a question of how many guesses.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-oneway

21. How long does guessing take?

Worked example

Work the estimate for SHA-256 one step at a time. Everything is in powers of two, so multiplying means adding exponents.

stepreasoningresult
1. tries neededeach guess lands on one of 2^256 digests like a random dart; on average you hit y after about half of themabout 2^255 tries
2. world hash rateevery Bitcoin miner on Earth together does roughly 2^70 hashes per second2^70 per second
3. per yeara year is about 2^25 seconds2^70 x 2^25 = 2^95 tries per year
4. years needed2^255 divided by 2^952^160, about 10^48 years

Figure (svg): Log scale of years: the age of the universe at 10^10 and guessing at 10^48, 38 factors of ten further right

Reading powers of two: 2 to the 10 is about 1,000, so 2 to the 70 is about 10 to the 21 and 2 to the 160 is about 10 to the 48. Guessing is not a plan. The only shortcut would be a flaw in H itself, and for SHA-256 nobody has found one.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-oneway-cost

Check: redo step 4 in decimal

Why: 2 to the 255 is about 5.8 x 10 to the 76 and 2 to the 95 is about 4.0 x 10 to the 28; their ratio is about 1.46 x 10 to the 48, the same as 2 to the 160.

22. Redo the estimate for a 128-bit digest

Fill the middle

Same method, same world hash rate of 2 to the 95 tries per year, but now the digest is only 128 bits long (as for MD5).

Fill in the blanks

tries needed, about half of 2^128: 2^127
years = 2^127 / 2^95 = 2^32
2^32 is about 4.3 billion years

Why: Half of 2^128 is 2^127. Dividing by 2^95 subtracts exponents: 2^32 years, about 4.3 billion years, a third of the age of the universe. Even a digest half as long resists inversion by brute force. MD5 fell to collisions, which cost only a square root, not to inversion.

23. Property 2: second-preimage resistance

Concept

second-preimage resistance (weak collision resistance) — given an input x, it is hard to find a different input x' with H(x') = H(x)

Figure (svg): The real installer hashes to a published digest y; the attacker's modified installer must hash to exactly the same y

The attack it stops: Ubuntu publishes the digest of its installer, and an attacker wants to hand you a modified installer that matches it, so your check passes. The real file is already one preimage of that digest; the attacker needs a second one.

The cost is the same as property 1. The attacker is stuck with one target digest, fixed by a file they did not write, and must hit it exactly: about 2 to the n tries.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-second-pre

24. Property 3: collision resistance

Concept

collision resistance — it is hard to find any two different inputs x and x' with H(x) = H(x'); nothing is given, and the attacker chooses both

Figure (svg): A developer writes a clean app and a malware twin with the same digest; the store approves the clean one, then lets the twin through because only digests are compared

The difference from property 2 is freedom. The developer wrote both files and never needed one specific digest; any pair of their own that collides will do. That freedom makes the attack much cheaper: about 2 to the n/2 tries for a good hash, so 2 to the 128 for SHA-256, not 2 to the 256.

This is not hypothetical. The Flame malware (2012) used an MD5 collision to get code signed as if by Microsoft. Why the square root? The answer is a fact about birthdays, which is Part 3.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-collision

25. Which property is the attacker trying to break?

Sorting

Sort each attacker by the property that stands in their way.

Sort into buckets

Which property does each attacker need to defeat?

one-way
Holds a leaked database of password digests and wants to log in; Sees the digest of a secret recipe and wants to learn the recipe
second-preimage resistance
Wants a modified copy of an existing, already-published installer to pass the digest check
collision resistance
Writes a harmless document and a harmful one, and wants both to share a digest
ow
Handed a digest, wants an input behind it. Both the password thief and the recipe snoop are playing the inversion game.
sp
The target file, and so the target digest, is fixed by someone else. Only a second input for that exact digest helps.
cr
Nothing is fixed in advance; the attacker writes both inputs. This is the cheapest game, at about 2 to the n/2.

26. Part 3 — The birthday paradox

Section

Why finding a collision costs a square root

27. The birthday question

Commit first

Picture a lecture room with 80 students. Birthdays are spread over 365 days.

Predict first

What is the chance that at least two people in the room share a birthday?

  • Under 10%
  • About 20%
  • A bit over 50%
  • Nearly certain, above 99%

Correct: Nearly certain, above 99%

Why: The exact answer is 99.991%. Most people guess small, because 80 is much less than 365. The next slides compute the number step by step with no formula pulled out of the air. In a real room of 80, there are usually several shared birthdays.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-q

28. Step 1: flip the question

Concept

At least two people share is messy to count directly. Two could share, or three, or two separate pairs, or a triple and a pair. Too many cases.

All 80 birthdays are different is one clean event. Compute its probability instead, and subtract from 1.

assumptionwhy we need it
365 days, ignore February 29a fixed number of equally sized holes
every day equally likelyeach person is a uniformly random dart
birthdays are independentone person's birthday says nothing about another's

Real birthdays are not perfectly uniform, but close enough. Say these assumptions out loud: later we replace 365 days with 2 to the n digests, where a good hash makes them exactly true.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-1

29. Step 2: add the people one at a time

Concept

Line the 80 people up. Ask each one, in turn, to have a birthday that nobody before them has.

personmust avoidchance, given everyone before avoided each other
1nobody365/365 = 1.00000
21 day already taken364/365 = 0.99726
32 days already taken363/365 = 0.99452
43 days already taken362/365 = 0.99178
2322 days already taken343/365 = 0.93973
8079 days already taken286/365 = 0.78356

Person k must avoid k - 1 taken days, so their chance is (365 - k + 1)/365. Every single factor is close to 1; even the last is 78%. The surprise is what happens when you multiply 80 of them.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-2

30. Your turn: the next factors

Faded example

Continue the pattern: person k must avoid k - 1 taken days. Give each answer to 5 decimal places.

Fill in the blanks

person 5: (361)/365 = 0.98904
person 10: 356/365 = 0.97534

Why: Person 5 avoids 4 taken days: 361/365 = 0.98904. Person 10 avoids 9: 356/365 = 0.97534. Each factor shrinks by 1/365, about 0.00274, so they fall slowly. Multiplying many factors that are each a little below 1 is what drives the product down.

31. Step 3: multiply, and watch it collapse

Concept

P(all different) is the product of all 80 factors. Three lines of Python compute it, so nobody has to trust a trick.

p = 1.0
for k in range(80):
    p *= (365 - k) / 365      # person k+1 must avoid k taken days
print(1 - p)                  # 0.99991...
after this many peoplerunning product P(all different)so P(at least two share)
100.88312%
230.49351%: better than a coin flip
400.10989%
600.005999.4%
800.000085799.991%

Figure (svg): The probability that all birthdays differ falls steeply with the number of people, crossing one half at 23

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-3

32. Where does it cross one half?

Hypothesis

Before reading the table closely: the product is still 0.883 at 10 people.

Predict first

At roughly how many people does the chance of a shared birthday first pass 50%?

  • About 23
  • About 60
  • About 183, half of 365
  • About 365

Correct: About 23

Why: At 22 people the chance is 47.6%; at 23 it is 50.7%. Halfway through the year, 183, is the intuition for hitting one particular day. A shared birthday is not about one particular day; it is about any pair, and pairs grow much faster than people.

33. Why it feels wrong: count pairs, not people

Concept

Your gut compares 80 people with 365 days, sees that 80 is much smaller, and says unlikely. But a shared birthday is a property of a pair.

peoplepairs = T(T - 1)/2expected matching pairs = pairs / 365chance of a shared birthday
10450.1212%
232530.6951%
304351.1971%
501,2253.3697%
803,1608.6699.99%

80 people make 3,160 pairs, each matching with chance 1/365, so we should expect about nine matching pairs in the room, not zero. Pairs grow like the square of the number of people. Rule of thumb: with K possible values, the chance of a collision passes 50% after about 1.2 x the square root of K samples. The square root of 365 is about 19, and 1.2 x 19 is about 23.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-4

34. Your turn: pairs in a room of 23

Fill the middle

Count the pairs, then the expected number of matching pairs.

Fill in the blanks

pairs = 23 x 22 / 2 = 253
expected matches = 253 / 365 = 0.69 (to 2 decimal places)

Why: 23 x 22 = 506, halved is 253 pairs. 253/365 = 0.69 expected matching pairs. An expected count near 0.7 fits a probability near one half. The pair count, not the head count, is what to compare with 365.

35. The same computation for any K and T

Concept

Now the general version: T people, each birthday drawn at random from K days. Count outcomes two ways.

all outcomes:     K x K x K x ... x K            = K^T
no repetition:    K x (K-1) x (K-2) x ... x (K-T+1)

P(no repetition) = K/K x (K-1)/K x (K-2)/K x ... x (K-T+1)/K
                 = (1 - 0/K)(1 - 1/K)(1 - 2/K) ... (1 - (T-1)/K)
termmeaning
K^Tevery person may pick any of the K days
K(K-1)...(K-T+1)each person must avoid the days already taken (the falling factorial)
(1 - i/K)the factor for person i + 1, the same fractions as for the room

With K = 365 and T = 80 these are exactly the 80 fractions from before. A product is hard to read, so two approximations turn it into a formula we can solve.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-recap

36. Approximation 1: bound the product by an exponential

Concept

One inequality does the work: 1 - x is never larger than e to the minus x, for every x. The curve and the line touch only at x = 0.

Figure (svg): Plot of e to the minus x and the line 1 minus x: the curve is always on or above the line

(1 - 1/K)(1 - 2/K) ... (1 - (T-1)/K)
   <=  e^(-1/K) x e^(-2/K) x ... x e^(-(T-1)/K)
    =  e^(-(1 + 2 + ... + (T-1)) / K)        exponents add
    =  e^(-T(T-1) / 2K)                      the sum is T(T-1)/2
quantitybound
P(no repetition)at most e^(-T(T-1)/2K)
P(some repetition)at least 1 - e^(-T(T-1)/2K)

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-exp

37. Checkpoint: which way does the bound go?

Check

Directions matter for a security argument.

Check your understanding

We showed P(no repetition) is at most e to the minus T(T-1)/2K. What does that tell us about the chance of a collision?

  • A. The true chance of a collision is at least 1 - e to the minus T(T-1)/2K (correct)
  • B. The true chance of a collision is at most 1 - e to the minus T(T-1)/2K
  • C. The true chance of a collision equals e to the minus T(T-1)/2K
  • D. Nothing: the bound only applies to birthdays

Answer: A

Why: Each factor is at most its exponential, so the product, P(no repetition), is at most the exponential. Subtracting from 1 flips the direction: P(some repetition) is at least 1 - e to the minus T(T-1)/2K. For an attacker that is good news: the formula guarantees at least this chance of success.

Why B tempts people
Subtracting from 1 flips an upper bound into a lower bound, not the other way round.
Why C tempts people
e to the minus T(T-1)/2K bounds the chance of no repetition, and it is a bound, not an equality.
Why D tempts people
Nothing in the derivation used birthdays specifically, only K equally likely values and independent draws.

38. Approximation 2: from e to the minus x to 1 - x, for small x only

Concept

Start from the Taylor series of e to the minus x, below. For small x, the terms after 1 - x are tiny, so e to the minus x is about 1 - x. With x = T(T-1)/2K, the chance of a collision is roughly T(T-1)/2K: it grows like T squared, because it counts pairs.

e^(-x) = 1 - x + x^2/2 - x^3/6 + ...      the Taylor series
       ~ 1 - x                             when x is small
T (K = 365)xexact P(no repetition)bound e^(-x)line 1 - x
100.1230.8830.8840.877
230.6930.4930.5000.307
402.1370.1090.118-1.137
808.6580.000090.00017-7.658

The exponential bound sits just above the exact value everywhere. The straight line fails once T passes about the square root of K, which is 19 here: at 23 people it says 31% instead of 49%, and at 40 it goes negative, which no probability can be. So the exponential form is the one to solve.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-bday-linear

39. A probability below zero?

Anomaly

At T = 40, the line 1 - T(T-1)/2K gives -1.137 for the chance that all birthdays differ.

Predict first

What went wrong?

  • The arithmetic: T(T-1)/2K is really below 1 at T = 40
  • The approximation e to the minus x = 1 - x is only good for small x, and here x = 2.137
  • The birthday model breaks down above 23 people
  • Nothing: probabilities can be negative in this model

Correct: The approximation e to the minus x = 1 - x is only good for small x, and here x = 2.137

Why: Dropping the x squared/2 and later terms is harmless when x is tiny, but at x = 2.137 the dropped terms are larger than what was kept. The exact product (0.109) and the exponential bound (0.118) both remain sensible. Always check that an approximation is used inside the range where it holds.

40. From birthdays to hashing

Concept

Same problem, new names. Days become digests, people become the inputs we hash, and a shared birthday becomes a collision.

birthdayshashing
K = 365 possible daysK = 2^n possible digests
T peopleT inputs x, x', x'', ... that we hash
two people share a birthdaytwo inputs share a digest: a collision
every day equally likelythe hash behaves like a random function: every digest equally likely

The birthday bound applies word for word: put K = 2 to the n into 1 - e to the minus T(T-1)/2K, and that is a lower bound on the chance of a collision among T inputs. It is a lower bound: the true chance of a collision is at least this. A good hash is designed to behave like a random function, so the uniform assumption is exactly the design goal.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-recap

41. Translate the birthday story

Translation

Match each birthday idea to its hashing counterpart.

Match the pairs

  • b1. 365 days
  • b2. a person walking in
  • b3. a shared birthday
  • b4. birthdays are uniform and independent
  • h1. 2 to the n possible digests
  • h2. one more input hashed by the attacker
  • h3. a collision
  • h4. the hash behaves like a random function

Why: With this dictionary, everything computed for the room transfers to hashing. The attacker hashes many inputs of their own and waits for any two to agree, just as a room fills with people until two share a day.

42. How many inputs until the first collision?

Concept

Choose the success probability p we want, set the bound equal to p, and solve for T.

1 - e^(-T^2 / 2K) = p             for large T, T(T-1) is about T^2
       T^2 / 2K = ln(1 / (1-p))
              T = sqrt(2 ln(1/(1-p))) x sqrt(K)  =  c(p) x sqrt(K)
with K = 2^n:   T = c(p) x 2^(n/2)
target pc(p)T for K = 365
1%0.143 people
50%1.1823 people
99%3.0358 people

Whatever p we pick, T is a small constant times the square root of K. Measured in units of the square root of K, the curve is the same for 365 days and for 2 to the 256 digests: at 1.18 square roots the chance is a coin flip, at 2 it is 86%, at 3 it is 99%. A birthday search costs about 2 to the n/2, not 2 to the n. For SHA-1 that is 2 to the 80, not 2 to the 160.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-solve

43. See it happen: a simulated 24-bit digest

Concept

You can check the square-root law without any hash function at all. Draw random 24-bit values, as a random function would produce, and count how many draws until two agree. This is pure probability, the same experiment as filling a room.

import random
K = 2**24                       # a 24-bit digest: about 16.8 million values
def draws_until_repeat():
    seen = set()
    while True:
        v = random.randrange(K)
        if v in seen:
            return len(seen) + 1
        seen.add(v)
quantityvalue
possible values K16,777,216
square root of K4,096
theory: 1.18 x sqrt(K), for a 50% chanceabout 4,823 draws
theory: average wait, sqrt(pi K / 2)about 5,134 draws
400 simulated runs: median and mean4,903 and 5,210

Sixteen million possible values, and a repeat typically shows up after about five thousand draws. Scale the same law to 256 bits and the wait becomes about 2 to the 128 draws, which is why SHA-256 is still safe.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-solve

44. The square root on real digest sizes

Concept

Two ways to find a collision. Fix one input and search for a second preimage: 2 to the n. Or hash many inputs and wait for any two to agree: the birthday search, 2 to the n/2.

Figure (svg): Bars for MD5, SHA-1 and SHA-256: the fixed-target cost is 2^128, 2^160 and 2^256, the birthday cost is 2^64, 2^80 and 2^128

digestnfixed targetbirthday searchcollision security
MD51282^1282^6464 bits
SHA-11602^1602^8080 bits
SHA-2562562^2562^128128 bits

2 to the 64 is within reach: the lecture estimates ten gaming GPUs running for about a year, roughly 20,000 US dollars in 2026, down from about 100,000 in 2017. So MD5 was doomed by its size alone, before any clever attack. An n-bit digest gives only n/2 bits of collision security, and that is why today's digests are 256 bits long.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-cost

45. Checkpoint: choosing a digest length

Check

Solve it on paper before you click.

Check your understanding

A design needs 128 bits of collision security, meaning a birthday search must cost about 2 to the 128 hashes. What is the shortest digest that meets this?

  • A. 128 bits
  • B. 160 bits
  • C. 256 bits (correct)
  • D. 512 bits

Answer: C

Why: A birthday search costs 2 to the n/2, so 2 to the n/2 = 2 to the 128 gives n = 256. This is why SHA-256 is the standard choice: its 256 bits give 128 bits of collision security, matching the 128-bit security level of AES-128.

Why A tempts people
A 128-bit digest gives only 2 to the 64 collision security, the level that doomed MD5.
Why B tempts people
SHA-1's 160 bits give 2 to the 80 collision security, short of 2 to the 128 even before any cryptanalysis.
Why D tempts people
512 bits works but is more than the minimum asked for; it gives 256 bits of collision security.

46. Part 4 — How the properties relate

Section

Building functions that keep one property and break another

47. One-way but not collision resistant?

Counterexample

Suppose g is a good one-way hash. Your task is to build a new hash h out of g that is still one-way, but where anyone can find a collision instantly.

Discussion prompt

Describe a one-line construction of h from g, and argue both halves: why h stays one-way, and why collisions in h are free.

Hint: What if h ignored part of its input?

Answer:

Define h(x) = g(x with its last bit removed). For every x, the inputs x0 and x1 (x followed by 0, and x followed by 1) give the same digest, because the last bit is thrown away before g ever sees it.

Figure (svg): Inputs x0 and x1 both lose their last bit, become x, and hash to the same g(x)

h stays one-way: a preimage of h is a preimage of g with one extra bit tacked on, so inverting h means inverting g. The technique, building a deliberately odd function that keeps one property and breaks another, is how you show one property does not imply another.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-oneway-vs

48. Collision resistant but not one-way?

Counterexample

Now the other direction. Suppose g is collision resistant with an n-bit output. Build h from g so that collisions stay hard, but half of all digests can be inverted with no work at all.

Discussion prompt

How could h let some inputs through in the clear without creating collisions?

Hint: Copying an input can never collide.

Answer:

Give h an (n+1)-bit output. If x is exactly n bits long, h(x) = 0 followed by x itself. Otherwise h(x) = 1 followed by g(x).

Figure (svg): n-bit inputs are copied with a leading 0; all other inputs are hashed by g with a leading 1

Collisions stay hard: two different n-bit inputs give different copies, two other inputs collide only if g does, and the first bit keeps the two kinds apart. But h is not one-way: half of all (n+1)-bit digests start with 0, and each of those is inverted by deleting the 0. So neither property implies the other.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-not-oneway

49. Weak and strong collision resistance, side by side

Concept

Second-preimage resistance has a second name, weak collision resistance. Plain collision resistance is then the strong kind. Comparing them carefully sets up a question.

weak (second preimage)strong (collision resistance)
attacker is givena randomly chosen xnothing
attacker must findx' different from x with h(x') = h(x)any pair x, x' with the same digest
can the attacker pick a convenient x?noyes, both inputs
generic costabout 2^nabout 2^(n/2)

The weak attacker is stuck with one random target. The strong attacker picks both inputs, and that freedom is worth a square root. Question: does weak collision resistance imply the strong kind?

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-weak-cr

50. Build a hash that is weak-resistant but not strong-resistant

Socratic

Start from a good hash g. You want h to resist second preimages for random inputs, yet have a collision anyone can write down.

Discussion prompt

How can you give h a collision that everyone knows, while a random x almost never runs into it?

Hint: Think about a single special case written into the definition.

Answer:

Hard-wire one collision. Pick two fixed inputs a and b, write them into the definition of h, and let h(x) = g(x) for every x except b, with h(b) = g(a). The pair (a, b) is a collision, printed in the definition.

The next slide checks that this h really keeps the weak property while losing the strong one.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-weak-cr

51. Why weak does not imply strong: plant one collision

Concept

Figure (svg): h equals g everywhere except at the fixed input b, which is sent to g(a), the digest of the other fixed input

propertydoes h have it?why
weak collision resistanceyesa random x is almost never a or b, so a second preimage for it is a genuine collision in g, and g is good
collision resistancenothe pair (a, b) is in the definition; the strong attacker hands it in with zero work

Not only a thought experiment. MD5 is in this state today: collisions take seconds on a laptop, while the best second-preimage attack still needs about 2 to the 123 work. Broken can mean two very different things, so always ask: broken for which property?

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-weak-vs-strong

52. Does strong imply weak?

Explain it to yourself

Suppose h is collision resistant (the strong property). Must it also be weakly collision resistant?

Discussion prompt

Argue it before reading on. Hint at the method: suppose someone had a fast program A that breaks the weak property. Could you use A to break the strong one?

Hint: A collision finder is allowed to choose its own x.

Answer:

Yes. If A, given a random x, returns x' different from x with h(x') = h(x), then you can find a collision: pick a random x yourself, run A on it, and output the pair (x, x'). That pair is a collision.

So if collisions are hard to find, no such fast A can exist. Strong implies weak. The next slide writes this out properly; it is the first reduction proof of the course.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-implies-weak

53. A first reduction proof

Concept

A reduction turns an attacker against one property into an attacker against another. Here we build a collision finder B out of any weak attacker A.

Figure (svg): B picks a random x, hands it to A, receives a second preimage x', and outputs the pair as a collision

suppose A breaks weak collision resistance:
    given a random x, A returns x' != x with h(x') = h(x)
then B breaks collision resistance:
    pick a random x; run A on x to get x'; output (x, x')
proof stepwhy it holds
B may choose xthe strong game hands B nothing, so B picks x itself
B picks x at randomthat is exactly the input A expects, so A succeeds as often as usual
(x, x') is a collisionx' differs from x and has the same digest
contrapositivecollision resistant implies weakly collision resistant

Together with the planted collision: strong implies weak, and weak does not imply strong. The pattern, assume an attacker and build another from it, recurs all term.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-cr-implies-weak

54. Put the reduction in order

Ranking

Order the steps of the argument that collision resistance implies weak collision resistance.

Put in order

  1. Assume a fast attacker A that, given a random x, finds a second preimage
  2. Build B: B picks a random x
  3. B runs A on x and receives x' with the same digest
  4. B outputs (x, x'), a collision, at the cost of one run of A
  5. Conclude: if no fast collision finder exists, no fast A exists either

Why: Assume the attacker, build the new attacker around it, show the output is valid and the cost is small, then take the contrapositive. Every reduction in the course has this shape.

55. Two truths and a myth about the properties

Two truths and a lie

Two of these are true and one is a myth.

Eliminate the wrong options

Which statement is the myth?

  • t1. Weak collision resistance implies collision resistance
  • t2. Collision resistance implies weak collision resistance
  • t3. A hash can be one-way and still have collisions that anyone can find instantly

Survives elimination: t1

Why: The planted-collision construction keeps the weak property and loses the strong one, so weak does not imply strong. MD5 is the real-world example: trivial collisions, no practical second preimages.

56. Part 5 — Where hashes are used

Section

Say which property you are relying on

57. Hashing is not encryption

Concept

Students mix these up constantly, so set them side by side.

hashencryption
a key?none: anyone can compute H(x) from xyes: you need the key to encrypt and decrypt
reversible?no: there is no un-hashing, the input is goneyes: decrypt with the key and the message is back
same input twice?same digest, always, so digests can be compareda different ciphertext each time, on purpose
exampleSHA-256(hello) is always 2cf2...9824AES-encrypting hello looks different every time

Row 3 is the one to push on. Determinism is a feature for a hash, because it lets you compare digests, and a fatal flaw for a cipher, because equal ciphertexts reveal equal messages. A hash hides nothing by itself; it only makes forgery hard.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-vs-enc

58. Hash or encrypt?

Sorting

Decide which tool each requirement needs.

Sort into buckets

Which tool fits each requirement?

hash
Check that two large files are identical without sending either one; Let a server recognise a password without being able to recover it
encryption
Get the original document back later, but keep it unreadable to others; Send a message only the recipient can read
hash
You only need to compare or recognise, never to recover. A deterministic, keyless fingerprint is exactly right.
enc
You need the content back, or need to hide it from some people but not others. Only a keyed, reversible scheme does that.

59. Application 1: storing passwords

Concept

Figure (svg): The user's password is hashed and compared with the stored digest; a stolen file contains only digests, but a thief can still hash guesses

questionanswer
why hash instead of encrypt?an encryption key would have to live on the server; steal the server and you decrypt every password. A hash has no key to steal.
does it protect weak passwords?no: the attacker hashes 123456, password, hunter2 and compares. Hashing stops reading, not guessing.

The second answer motivates salting and deliberately slow hashes, which come later in the course with password hashing.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-pw

60. Which property does password storage rely on?

Elimination

The thief holds H(password) and wants to log in.

Eliminate the wrong options

Which property is the one that stops them?

  • p1. One-wayness
  • p2. Weak collision resistance
  • p3. Collision resistance

Survives elimination: p1

Why: The thief has a digest and wants any input that produces it: that is exactly the one-way game. Then repeat the catch: one-wayness stops reading, not guessing, so a weak password still falls to a guessing attack.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-pw-q

61. Trap: hashed means safe

Trap

The trap

A common belief: once passwords are stored as SHA-256 digests, a leak of the password file reveals nothing.

stored:  H("123456")  = 8d969eef...6c92
claimreality
SHA-256 is one-waytrue
so the leak reveals nothingfalse: the attacker hashes common passwords and compares
so 123456 is safe once hashedfalse: it is one of the first guesses tried

The fix

One-wayness rules out working backwards from the digest. It does nothing against guessing forwards, and guessable inputs are guessed. Real password storage adds a random salt per user and a deliberately slow hash, so each guess costs the attacker far more.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-pw

62. Application 2: checking a download

Concept

Ubuntu wants you to get its 6 GB installer unmodified. The file comes from any mirror; the digest comes from ubuntu.com.

Figure (svg): The digest travels from ubuntu.com over HTTPS while the 6 GB file comes from an untrusted mirror; you hash the file and compare

The trick: a trusted channel for 256 bits protects 6 GB. You only need to get the tiny digest from a place the attacker cannot touch. So the check is exactly as trustworthy as ubuntu.com itself, and no more. Every signed update and certificate chain later in the course is this picture with the website replaced by a public key.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-integrity

63. Which property does the download check rely on?

Elimination

The attacker wants to swap the installer without changing its published digest.

Eliminate the wrong options

Which property is the one that stops them?

  • d1. Weak collision resistance (second preimage)
  • d2. One-wayness
  • d3. Nothing: the attacker can always swap the file

Survives elimination: d1

Why: The file and its digest are fixed, so the attacker needs a second preimage. But if the attacker helped write the file, say through a contributed library or the build system, they chose both inputs and we are back in the strong game. That is why anything signed should use a fully collision-resistant hash.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-integrity-q

64. Application 3: a sealed bid

Concept

An auction where everyone bids at once, nobody can see the others' bids, and nobody can change their bid afterwards. Alice posts only a digest of her bid together with a random value r.

Figure (svg): Four steps: commit by posting H(bid, r); wait while a table of digests reveals nothing; open by revealing bid and r so everyone re-hashes; a changed bid would need the same digest

commit:  SHA-256("bid=500;r=3f9c2a71e4b8d605") = 6ad43f63...b3371d
open:    reveal 500 and r; everyone recomputes the same digest
cheat?:  SHA-256("bid=400;r=3f9c2a71e4b8d605") = 75000e56...a5c279  no match
goalwhat provides it
hiding: nobody can read the bidone-wayness plus the random r
binding, afterwards: Alice cannot switch to another bida different bid with the same digest is a second preimage
binding, in advance: no two prepared bidstwo bids with one digest is a collision

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-commit

65. Why does the bid need the random r?

Socratic

Suppose Alice posted just SHA-256("500"), with no random value.

Discussion prompt

SHA-256 is one-way. So why is a bare digest of the bid not enough to hide it?

Hint: How many plausible bids are there?

Answer:

Bids are guessable. There are only a few plausible amounts, so anyone can hash 100, 200, 300 and so on and read every bid off the table by comparing digests.

One-wayness stops working backwards, not guessing forwards; this is the same lesson as passwords. Mixing in a long random r makes the input unguessable, so the digest finally hides the bid. Alice reveals r when she opens.

66. Which properties does the sealed bid need?

Discrimination

Alice hands in H("500" + r). For each property, decide whether the scheme relies on it.

Sort into buckets

Does the sealed bid rely on each property?

needed
one-wayness; weak collision resistance; collision resistance
not needed (or not even a property of hashes)
the hash being reversible with a key
yes
One-wayness with r hides the bid; weak collision resistance stops Alice re-opening to a different bid; collision resistance stops her preparing two bids with one digest in advance. Alice chose the input, so the strong property is in play.
no
Hashes have no key and cannot be reversed. A scheme that needed reversal would need encryption, not a hash.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-commit-q

67. Each use, each property

Comparison

Fill in the summary table: does each use rely on each property?

Comparison matrix

useone-wayweak collision resistancecollision resistance
password storageyesnono
download check (file written by someone else)noyesno
download check (attacker helped write it)noyesyes
sealed bidyesyesyes

Use a hash is not a design. Say which property you are relying on, and check what the attacker gets to choose.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-commit-q

68. Checkpoint: name the game

Check

Solve it on paper before you click.

Check your understanding

A Git repository names every file by its hash. An attacker contributes a harmless file to a project and later wants to swap in a different file with the same name (the same hash). Which property must the hash have to stop this?

  • A. One-wayness
  • B. Weak collision resistance only
  • C. Collision resistance (correct)
  • D. None: hashes cannot protect this

Answer: C

Why: The attacker wrote the harmless file, so they chose both inputs. That is the collision game, the cheapest one, at about 2 to the n/2. This is exactly why the SHA-1 collision of 2017 worried Git users and why Git has been adding SHA-256.

Why A tempts people
Nobody needs to work backwards; the attacker knows both files.
Why B tempts people
Weak resistance protects a file someone else wrote. Here the attacker authored the original.
Why D tempts people
A collision-resistant hash does protect it; the risk comes from a hash, like SHA-1, whose collision resistance is broken.

69. Checkpoint: reading the cost

Check

Solve it on paper before you click.

Check your understanding

A hash has a 200-bit digest and behaves like a random function. Roughly how many hashes does a generic birthday search need to find a collision?

  • A. 2 to the 200
  • B. 2 to the 199
  • C. 2 to the 100 (correct)
  • D. 2 to the 14, the square root of 200

Answer: C

Why: A birthday search costs about the square root of the number of digests: the square root of 2 to the 200 is 2 to the 100. Halve the exponent; do not take the square root of the bit length.

Why A tempts people
2 to the 200 is the cost of hitting one fixed target, the second-preimage game.
Why B tempts people
2 to the 199 is the average cost of inverting a digest by guessing, also a fixed-target game.
Why D tempts people
The square root applies to the number of possible digests, 2 to the 200, not to the number 200.

70. Draw the whole story

Connect it up

Pull the lesson together on one page.

Draw it

Draw the three properties as three games: for each, show what the attacker is given, what they must find, and the generic cost. Add arrows for the implications you proved (and a crossed-out arrow for the one that fails). Then attach each application, passwords, downloads and sealed bids, to the property or properties it relies on.

A complete sketch shows one-way and second preimage at about 2 to the n and collisions at 2 to the n/2; an arrow from collision resistance to weak collision resistance, with the reverse crossed out; one-wayness and collision resistance independent of each other; and passwords on one-wayness, downloads on second preimage (collision resistance if the attacker wrote the file), and sealed bids on all three.

71. Exit ticket

Exit ticket

One question to check the main idea stuck.

Predict first

Why does an n-bit hash give only about n/2 bits of collision security?

  • Because half of the digest bits are predictable
  • Because an attacker who chooses both inputs only needs any two of many hashes to agree, and the number of pairs grows like the square of the number of hashes
  • Because collisions are found by inverting the hash twice
  • Because the pigeonhole principle guarantees a collision after 2 to the n/2 inputs

Correct: Because an attacker who chooses both inputs only needs any two of many hashes to agree, and the number of pairs grows like the square of the number of hashes

Why: This is the birthday paradox: T hashes make about T squared/2 pairs, each matching with chance 1/2 to the n, so collisions become likely around T = 2 to the n/2. The pigeonhole principle only guarantees a collision after 2 to the n + 1 inputs; the birthday bound says one is likely far sooner.

72. What you can do now

Recap

The lesson's thread: a hash must have collisions, so security is about what an attacker can find; there are three properties, each a game with its own cost; choosing both inputs is worth a square root; and every use of a hash rests on specific properties that you should be able to name.

you needpropertygeneric cost
nobody can work back from a digestone-way2^n
nobody can match a file someone else wrotesecond-preimage resistance2^n
nobody can make any two inputs agreecollision resistance2^(n/2)

Next lesson: how MD5 and SHA-1 are actually built, and how their collision resistance broke.

Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-commit-q

Sources

  1. Security I, Lecture 2: Cryptography, part one (cryptographic hash functions)
  2. FIPS 180-4 — Secure Hash Standard (NIST, 2015)

Want this taught 1-on-1? Alexander tutors Computer Security — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108