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
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
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
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.
| lesson | topic | the question it answers |
|---|---|---|
| 1 (this one) | Hash functions | what does a fingerprint promise, and how hard is it to fake? |
| 2 | Inside MD5 and SHA-1 | how does a fixed-size box hash a file of any size, and how did MD5 and SHA-1 fall? |
| 3 | MACs and HMAC | how do we get integrity and authentication over a hostile network? |
| 4 | One-time pad and DES | what is perfect secrecy, and how do block ciphers shrink the key? |
| 5 | AES | what happens inside the cipher everyone uses? |
| 6 | Modes of operation | how do we encrypt more than one block, safely? |
| 7 | CPA security, encryption plus MAC | what does secure encryption mean, and how do we combine it with a MAC? |
| 8 | Public-key cryptography | how 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
Section
Any file in, a short fixed-size number out, and nobody can fake it
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.
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
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
| use | the short value stands in for | why that helps |
|---|---|---|
| check a download | a 6 GB installer | comparing 64 characters is instant; comparing 6 GB is hopeless |
| store a password | the password itself | the site recognises you, but a thief who steals the database gets digests |
| seal a bid | a bid you will reveal later | nobody can read it now, and you cannot change it afterwards |
Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-hash-why
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.
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| observation | what it means |
|---|---|
| fixed size | 64 hex digits every time; a hex digit is 4 bits, so 64 x 4 = 256 bits |
| deterministic | hello gives the same digest on any machine, any day, so digests can be compared |
| avalanche | hello and hellp differ in one letter, yet the digests look unrelated |
| no key | anyone 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
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?
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.
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 length | number of possible digests |
|---|---|
| 1 bit | 2 |
| 2 bits | 4 (00, 01, 10, 11) |
| 3 bits | 8 |
| 256 bits | 2 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
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
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.
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.
| quantity | size |
|---|---|
| possible SHA-256 digests | 2^256, about 1.16 x 10^77 |
| atoms in the visible universe | about 10^80 |
| possible inputs | infinitely 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
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?
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.
Section
Each one is the promise that one attack does not work
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
Matching
Match each use to the cheat that would ruin it.
Match the pairs
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
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.
| property | attacker is given | attacker must find | best attack if H is good |
|---|---|---|---|
| 1. one-way | a digest y | any x with H(x) = y | about 2^n tries |
| 2. second-preimage resistant | an input x | x' different from x with H(x') = H(x) | about 2^n tries |
| 3. collision resistant | nothing | any 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
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.
| attack | generic cost | SHA-256 (n = 256) | SHA-1 (n = 160) | MD5 (n = 128) |
|---|---|---|---|---|
| invert a digest | 2^n | 2^256 | 2^160 | 2^128 |
| second preimage | 2^n | 2^256 | 2^160 | 2^128 |
| any collision | 2^(n/2) | 2^128 | 2^80 | 2^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
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
Worked example
Work the estimate for SHA-256 one step at a time. Everything is in powers of two, so multiplying means adding exponents.
| step | reasoning | result |
|---|---|---|
| 1. tries needed | each guess lands on one of 2^256 digests like a random dart; on average you hit y after about half of them | about 2^255 tries |
| 2. world hash rate | every Bitcoin miner on Earth together does roughly 2^70 hashes per second | 2^70 per second |
| 3. per year | a year is about 2^25 seconds | 2^70 x 2^25 = 2^95 tries per year |
| 4. years needed | 2^255 divided by 2^95 | 2^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.
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.
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
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
Sorting
Sort each attacker by the property that stands in their way.
Sort into buckets
Which property does each attacker need to defeat?
Section
Why finding a collision costs a square root
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?
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
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.
| assumption | why we need it |
|---|---|
| 365 days, ignore February 29 | a fixed number of equally sized holes |
| every day equally likely | each person is a uniformly random dart |
| birthdays are independent | one 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
Concept
Line the 80 people up. Ask each one, in turn, to have a birthday that nobody before them has.
| person | must avoid | chance, given everyone before avoided each other |
|---|---|---|
| 1 | nobody | 365/365 = 1.00000 |
| 2 | 1 day already taken | 364/365 = 0.99726 |
| 3 | 2 days already taken | 363/365 = 0.99452 |
| 4 | 3 days already taken | 362/365 = 0.99178 |
| 23 | 22 days already taken | 343/365 = 0.93973 |
| 80 | 79 days already taken | 286/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
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.
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 people | running product P(all different) | so P(at least two share) |
|---|---|---|
| 10 | 0.883 | 12% |
| 23 | 0.493 | 51%: better than a coin flip |
| 40 | 0.109 | 89% |
| 60 | 0.0059 | 99.4% |
| 80 | 0.0000857 | 99.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
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%?
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.
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.
| people | pairs = T(T - 1)/2 | expected matching pairs = pairs / 365 | chance of a shared birthday |
|---|---|---|---|
| 10 | 45 | 0.12 | 12% |
| 23 | 253 | 0.69 | 51% |
| 30 | 435 | 1.19 | 71% |
| 50 | 1,225 | 3.36 | 97% |
| 80 | 3,160 | 8.66 | 99.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
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.
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)| term | meaning |
|---|---|
| K^T | every 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
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| quantity | bound |
|---|---|
| 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
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?
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.
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) | x | exact P(no repetition) | bound e^(-x) | line 1 - x |
|---|---|---|---|---|
| 10 | 0.123 | 0.883 | 0.884 | 0.877 |
| 23 | 0.693 | 0.493 | 0.500 | 0.307 |
| 40 | 2.137 | 0.109 | 0.118 | -1.137 |
| 80 | 8.658 | 0.00009 | 0.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
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?
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.
Concept
Same problem, new names. Days become digests, people become the inputs we hash, and a shared birthday becomes a collision.
| birthdays | hashing |
|---|---|
| K = 365 possible days | K = 2^n possible digests |
| T people | T inputs x, x', x'', ... that we hash |
| two people share a birthday | two inputs share a digest: a collision |
| every day equally likely | the 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
Translation
Match each birthday idea to its hashing counterpart.
Match the pairs
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.
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 p | c(p) | T for K = 365 |
|---|---|---|
| 1% | 0.14 | 3 people |
| 50% | 1.18 | 23 people |
| 99% | 3.03 | 58 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
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)| quantity | value |
|---|---|
| possible values K | 16,777,216 |
| square root of K | 4,096 |
| theory: 1.18 x sqrt(K), for a 50% chance | about 4,823 draws |
| theory: average wait, sqrt(pi K / 2) | about 5,134 draws |
| 400 simulated runs: median and mean | 4,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
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
| digest | n | fixed target | birthday search | collision security |
|---|---|---|---|---|
| MD5 | 128 | 2^128 | 2^64 | 64 bits |
| SHA-1 | 160 | 2^160 | 2^80 | 80 bits |
| SHA-256 | 256 | 2^256 | 2^128 | 128 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
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?
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.
Section
Building functions that keep one property and break another
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
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
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 given | a randomly chosen x | nothing |
| attacker must find | x' different from x with h(x') = h(x) | any pair x, x' with the same digest |
| can the attacker pick a convenient x? | no | yes, both inputs |
| generic cost | about 2^n | about 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
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
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
| property | does h have it? | why |
|---|---|---|
| weak collision resistance | yes | a 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 resistance | no | the 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
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
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 step | why it holds |
|---|---|
| B may choose x | the strong game hands B nothing, so B picks x itself |
| B picks x at random | that is exactly the input A expects, so A succeeds as often as usual |
| (x, x') is a collision | x' differs from x and has the same digest |
| contrapositive | collision 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
Ranking
Order the steps of the argument that collision resistance implies weak collision resistance.
Put in order
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.
Two truths and a lie
Two of these are true and one is a myth.
Eliminate the wrong options
Which statement is the myth?
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.
Section
Say which property you are relying on
Concept
Students mix these up constantly, so set them side by side.
| hash | encryption | |
|---|---|---|
| a key? | none: anyone can compute H(x) from x | yes: you need the key to encrypt and decrypt |
| reversible? | no: there is no un-hashing, the input is gone | yes: decrypt with the key and the message is back |
| same input twice? | same digest, always, so digests can be compared | a different ciphertext each time, on purpose |
| example | SHA-256(hello) is always 2cf2...9824 | AES-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
Sorting
Decide which tool each requirement needs.
Sort into buckets
Which tool fits each requirement?
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
| question | answer |
|---|---|
| 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
Elimination
The thief holds H(password) and wants to log in.
Eliminate the wrong options
Which property is the one that stops them?
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
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| claim | reality |
|---|---|
| SHA-256 is one-way | true |
| so the leak reveals nothing | false: the attacker hashes common passwords and compares |
| so 123456 is safe once hashed | false: it is one of the first guesses tried |
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
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
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?
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
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| goal | what provides it |
|---|---|
| hiding: nobody can read the bid | one-wayness plus the random r |
| binding, afterwards: Alice cannot switch to another bid | a different bid with the same digest is a second preimage |
| binding, in advance: no two prepared bids | two bids with one digest is a collision |
Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-commit
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.
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?
Security I, Lecture 2: Cryptography, part one (cryptographic hash functions) slide s-commit-q
Comparison
Fill in the summary table: does each use rely on each property?
Comparison matrix
| use | one-way | weak collision resistance | collision resistance |
|---|---|---|---|
| password storage | yes | no | no |
| download check (file written by someone else) | no | yes | no |
| download check (attacker helped write it) | no | yes | yes |
| sealed bid | yes | yes | yes |
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
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?
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.
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?
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.
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.
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?
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.
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 need | property | generic cost |
|---|---|---|
| nobody can work back from a digest | one-way | 2^n |
| nobody can match a file someone else wrote | second-preimage resistance | 2^n |
| nobody can make any two inputs agree | collision resistance | 2^(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
Want this taught 1-on-1? Alexander tutors Computer Security — $55/session, free consultation.