Hash 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.
A hash function does not “summarise” content as a person would: it scrambles it deterministically until every output bit depends on every input bit.
- 01
Preparation
The message is completed with padding that includes its length, so as to obtain a whole number of blocks.
- 02
Splitting into blocks
The message is divided into fixed-size blocks, for example 512 bits for SHA-256.
- 03
Initial state
The function starts from a fixed initial value defined in the standard.
- 04
Compression
Each block is combined with the current state through many rounds of logical and arithmetic operations.
- 05
Result
After the last block, the final state is the hash value.
Two architectures
| Construction | Used by | Characteristic |
|---|---|---|
| Merkle-Damgård | MD5, SHA-1, SHA-2 | The state passes from one block to the next via a compression function; it is subject to length-extension attacks |
| Sponge | SHA-3 | The message is “absorbed” into a large internal state and the value is “squeezed” out; it does not suffer from length extension |
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.
The pages of this topic
Back to the overviewHash functions: the maths
The birthday paradox and why security against collisions is half the length of the value, with the pigeonhole principle.
Hash functions: worked example
SHA-256 values of similar words and phrases, to see the avalanche effect, and the official test vectors.
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.