Lesson 12 of 15
Birthday Paradox & Hash Collisions
Birthday Paradox and Hash Collisions
How many people must be in a room before two of them share a birthday? Surprisingly, only 23 people are needed for a >50% chance. This counter-intuitive result — the birthday paradox — directly determines the security of hash functions.
Birthday Attack on Hash Functions
If a hash function produces -bit outputs ( possible values), how many random inputs do we need before finding two with the same hash?
The birthday paradox gives us:
For a 50% collision probability, the threshold is approximately:
Implications for Hash Security
| Hash size | Collision threshold | Security level |
|---|---|---|
| 32 bits | ~77,163 | Toy — trivially broken |
| 64 bits | ~5 billion | Weak — birthday in seconds |
| 128 bits | ~ | MD5 range — compromised |
| 256 bits | ~ | SHA-256 — considered secure |
Rule of thumb: an -bit hash provides only bits of collision resistance.
Your Task
Implement:
birthday_probability(n, hash_bits)— probability of at least one collision among hashes of sizehash_bitsbits:collision_threshold(hash_bits, probability=0.5)— minimum for collision probability :
Use import math for math.exp, math.log, math.sqrt, math.ceil.
Python runtime loading...
Loading...
Click "Run" to execute your code.