Hash functions: the maths
The birthday paradox and why security against collisions is half the length of the value, with the pigeonhole principle.
Collisions always exist
A hash function maps an infinite set of messages to a finite set of values. By the pigeonhole principle, different messages with the same value must exist. Security lies not in the impossibility of collisions but in the practical impossibility of finding them.
The birthday paradox
In a group of just 23 people the probability that two share a birthday exceeds 50%. It seems strange, but the number of possible pairs grows much faster than the number of people: with 23 people there are 253 pairs.
- 01
The principle
With N possible values, about √N attempts are needed to have a good chance of finding two equal values.
- 02
Applied to hash values
An n-bit value has 2^n possible values: a collision is found with about 2^(n/2) attempts.
- 03
MD5
128-bit value: theoretical collision security of 2^64, a number within reach of today’s computers even without flaws in the algorithm.
- 04
SHA-256
256-bit value: about 2^128 attempts are needed, a number beyond any existing computer.
Hash functions: worked example
SHA-256 values of similar words and phrases, to see the avalanche effect, and the official test vectors.
The pages of this topic
Back to the overviewHash functions: how they work
How a hash function compresses data of any length into a fixed value: blocks, internal state, the Merkle-Damgård construction and the sponge construction.
Hash functions: history
From the first cryptographic hash functions to the SHA family, from MD5’s success to collisions, to the competition that produced SHA-3.
Hash functions: weaknesses and attacks
MD5 and SHA-1 collisions, chosen-prefix attacks, length extension and precomputed tables for passwords.
Hash functions today
Where hash functions are used every day: digital signatures, certificates, downloads, passwords, blockchain, version control and digital evidence.
Hash functions: try it yourself
Hands-on exercises to compute hash values, observe the avalanche effect and estimate the birthday paradox.
Hash functions: frequently asked questions
Short answers to the most common doubts about cryptographic hash functions.