Hash functions: try it yourself
Hands-on exercises to compute hash values, observe the avalanche effect and estimate the birthday paradox.
Exercise 1: compute a hash value
$ echo -n "abc" | sha256sum
$ echo -n "abd" | sha256sumCompare the two values. How many hexadecimal digits match in the same position? On average it should be about one in sixteen, as for two random strings.
Exercise 2: the empty file
Create an empty file and compute its SHA-256 value: it must begin with e3b0c442. Then add a single space and compute it again.
Exercise 3: the birthday paradox
- 01
Compute
The probability that in a group of k people no pair shares a birthday is the product of (365 − i)/365 for i from 0 to k − 1.
- 02
Try with 23
The result is about 0.49: the probability of at least one match is therefore about 0.51.
- 03
Generalise
With 32-bit hash values, how many random values are needed for a 50% chance of a collision? About 77,000, that is of the order of the square root of 2^32.
Hash functions: frequently asked questions
Short answers to the most common doubts about cryptographic hash functions.
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: 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.