Skip to content
Trapdoor·Lab

Hash functions and the avalanche effect

1Toy

A fingerprint of any data

A hash function takes data of any length and returns a result of fixed length — SHA-256 always 32 bytes, that is 256 bits. It has no key and nothing secret, and the same text always gives the same hash, so anyone can recompute and compare it. Flip one input bit: on average half of the output bits change (the avalanche effect), so similar hashes tell you nothing about similar data.

SHA-256 live

12 / 1024 B

Limit 1 KB; an empty text has a hash too. We count UTF-8 bytes — “é” is 2 bytes.

Too long by -1012 B. Shorten the text — the toy works on small data.

95

Byte no. 11: 62 → 63 (hex)

Text after the bit flip
UTF-8Trapdoor Lac

Computing SHA-256…

2Break it

A truncated hash and the birthday paradox

A cryptographic hash function must make it infeasible to find two different inputs with the same hash (a collision) or an input with a given hash (a preimage). For an n-bit hash, the generic preimage attack needs 2n attempts on average. A collision is much cheaper: each new attempt can hit the hash of any earlier one, so about 2n/2 attempts are enough — the birthday paradox, which gives a group of 23 people a better than 50% chance of a shared birthday. That is why full SHA-256 offers about 128 bits of collision resistance and 256 bits of preimage resistance; a quantum computer running Grover’s algorithm would bring a preimage down to about 2128 attempts. In the toy, SHA-256 is real, but the bank compares only the first 16, 24 or 32 bits — and both attacks finish in your browser.

A hash cut to a few bytes

Toy implementation — do not use in productionSHA-256 is real, but the bank compares only a 24-bit hash cut from it. For real, compare all 256 bits.

Hash length

The bank approves a transfer by its hash: it computes SHA-256 and keeps only the first bytes, because a short code is easier to copy and compare.

Computing the transfer’s SHA-256…

3In the real world

Where hashes guard data

  • MD5: a rogue certificate authority (2008)Researchers used an MD5 chosen-prefix collision: a certificate signed by a real certificate authority had the same hash as their own CA certificate, so the signature fitted both and browsers would have trusted the rogue CA. More than 200 PlayStation 3 consoles did the computing.Source (MD5: a rogue certificate authority (2008))
  • SHA-1: SHAttered (2017)A CWI Amsterdam and Google team showed two different PDF files with the same SHA-1. The computation came to about nine quintillion SHA-1 computations — 6,500 CPU-years and 110 GPU-years.Source (SHA-1: SHAttered (2017))
  • GitGit names every object by its SHA-1 hash. After SHAttered, Git 2.13 and later use a hardened SHA-1 implementation by default, which is not vulnerable to that attack, and the project picked SHA-256 as its successor.Source (Git)
  • Abbreviated names in GitGit shows commits under abbreviated names. By default it picks their length from the approximate number of objects in the repository, so the abbreviations stay unique for some time (core.abbrev).Source (Abbreviated names in Git)
  • Ubuntu checksumsNext to the ISO images sits a SHA256SUMS file with their hashes and its GPG signature. A matching hash says the file was not altered or corrupted on the way; the signature says Ubuntu published the list of hashes.Source (Ubuntu checksums)
  • Digital signaturesA signature covers the document’s hash, so a collision lets a signature move to another document. NIST advises dropping SHA-1 wherever collisions are a real threat, such as creating signatures, and phases it out by 31 December 2030 in favour of SHA-2 and SHA-3.Source (Digital signatures)
4Take it to your project

Full SHA-256 instead of MD5, SHA-1 and short hashes

Good practice

For checksums, content identifiers and signatures, use SHA-256 (or SHA-3) from the platform library and compare the whole hash. If you must shorten it, remember that n bits give only about n/2 bits of collision resistance — a 128-bit hash means 64-bit security.

JavaScript
const bytes = new TextEncoder().encode(text);
const digest = new Uint8Array(
  await crypto.subtle.digest('SHA-256', bytes),
);
C#
byte[] digest = SHA256.HashData(Encoding.UTF8.GetBytes(text));
Anti-pattern

MD5 and SHA-1 have practical collisions — do not use them for signatures or for integrity checks when an attacker can supply the data. A short hash prefix as an identifier falls to the birthday paradox. A hash does not replace a MAC or password hashing either: a keyed message is authenticated by HMAC (the MAC and HMAC station), passwords by slow, salted Argon2id (the Password hashing station).

JavaScript
// ✕ SHA-1 has practical collisions (SHAttered)
const digest = await crypto.subtle.digest('SHA-1', bytes);
C#
// ✕ 32 hash bits: a 50% chance of a collision after about 77,000 IDs
string id = Convert.ToHexString(SHA256.HashData(data))[..8];