Cryptographic hash functions · 07 Try it yourself

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

terminale
$ echo -n "abc" | sha256sum
$ echo -n "abd" | sha256sum

Compare 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

  1. 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.

  2. 02
    Try with 23

    The result is about 0.49: the probability of at least one match is therefore about 0.51.

  3. 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.

Continue with
08 FAQ

Hash functions: frequently asked questions

Short answers to the most common doubts about cryptographic hash functions.

↑