On this page
A 32-bit hash has about 4.3 billion possible outputs, yet about 77,000 random inputs give even odds of a collision (birthday attack). That gap between 2ⁿ and roughly 2^(n/2) decides how long the hashes behind digital signatures must be.
What is a birthday attack on a hash function?
A hash function squeezes any input into a fixed-length output, called a digest. A collision is two different inputs with the same digest. For a hash with n output bits, a collision is expected after roughly 2^(n/2) evaluations, a figure called the birthday bound (birthday attack).
The name comes from the birthday problem: in a room of 23 people, the chance that two share a birthday is already above 50% (birthday problem). You are looking for any matching pair, not a match to your own birthday, and 23 people make 253 pairs. An attacker hashes random inputs and waits for any two digests to match. No flaw in the hash is needed.
How is the collision probability calculated?
With N possible digests (N = 2ⁿ) and k random inputs, the chance that all k differ is (1 − 1/N)(1 − 2/N)…(1 − (k − 1)/N). Each factor is close to e^(−i/N), so the product is about e^(−k(k−1)/2N). That gives the approximation on the birthday attack page:
p(k) ≈ 1 − e^(−k²/2N)
At k = √N = 2^(n/2), the exponent is −1/2, so p ≈ 1 − e^(−0.5) ≈ 0.39.
Setting p = 1/2 gives k ≈ √(2 ln 2 × N) ≈ 1.18√N.
Birthday check: N = 365, k = 23 gives an exponent of 23 × 22 / 730 ≈ ln 2, so p ≈ 0.5.
How many tries break a 32-, 64- or 128-bit hash?
Every figure below comes from p(k) ≈ 1 − e^(−k²/2N); the 50% values match the table in the birthday attack entry.
32-bit hash (N = 2³² ≈ 4.29 × 10⁹)
k = 10,000: k²/2N = 10⁸ / (8.59 × 10⁹) ≈ 0.0116, so p ≈ 1.2%.
k = 2¹⁶ = 65,536: p ≈ 39%.
k ≈ 77,000: p ≈ 50%.
k = 100,000: the exponent is about 1.16, so p ≈ 69%.
64-bit hash (N = 2⁶⁴ ≈ 1.84 × 10¹⁹)
k = 10⁹, a billion: the exponent is about 0.027, so p ≈ 2.7%.
k = 2³² ≈ 4.3 × 10⁹: p ≈ 39%.
k ≈ 5.1 × 10⁹: p ≈ 50%.
128-bit hash (N = 2¹²⁸ ≈ 3.4 × 10³⁸)
k = 10¹², a trillion: the exponent is about 1.5 × 10⁻¹⁵, so p is roughly one in 680 trillion.
k ≈ 2.2 × 10¹⁹, close to 2⁶⁴: p ≈ 50%.
A 128-bit hash looks like 2¹²⁸ of work but gives only about 2⁶⁴ against collisions.
Why is SHA-256 used instead of a 128-bit hash for signatures?
A digital signature usually signs the hash of a document. If an attacker can make two documents with the same hash, say a fair contract and a harmful one, a signature on the first also fits the second.
NIST states the rule directly: the collision resistance strength of a hash, in bits, is half its output size, and its table lists SHA-256 at 128 bits (NIST hash functions). A 128-bit hash would offer only 64 bits, the 2⁶⁴ figure above.
MD5 produces a 128-bit digest (RFC 1321). RFC 6151 cites an attack that finds MD5 collisions in about one minute on a standard notebook PC, and says MD5 "must not be used for digital signatures" (RFC 6151).
SHA-1's 160-bit output should need about 2⁸⁰ work; NIST lists its collision strength as below 80 bits and disallowed it for digital signatures at the end of 2013 (NIST hash functions). In February 2017, Google and the CWI Institute in Amsterdam announced the first practical SHA-1 collision, using about nine quintillion (2⁶³) SHA-1 computations, and urged a move to safer alternatives such as SHA-256 (Google Security Blog).
Flaws like those in MD5 and SHA-1 only lower the birthday bound, so designers choose an output long enough that half of it is still out of reach.
How do you answer a hash collision question?
Use this five-step check, built on the formula from the birthday attack entry.
Write N = 2ⁿ for an n-bit output.
How many tries? About 2^(n/2), or 1.18 × 2^(n/2) for a 50% chance.
Probability after k tries? Compute k²/2N, then p ≈ 1 − e^(−k²/2N); if k²/2N is tiny, p ≈ k²/2N.
Sanity-check: k = 2^(n/2) should give about 39%.
For security, quote collision strength as n/2 bits.
The same counting appears in hash table collisions and load factor. For the complement rule, see the Class 12 probability formulas; for more cryptography maths, modular exponentiation by square and multiply.
To revisit the probability ideas behind the birthday bound, the Fermi Concept Library covers every concept in mathematics and computing. If a practice answer comes out wrong, the Fermi Problem Library has a step-by-step solver that explains the reasoning, so you can see which step slipped.



