How Hashing Works

Two letters or a whole film go into SHA-256 and exactly 32 bytes come out, the same 32 every time, and one changed letter re-rolls nearly all of them. Inside, a 64-byte block is stirred through 64 rounds of shifting, rotating and adding until there is no way back. Then the famous family: MD5 and SHA-1, both broken, and the 9.2 quintillion tries it took to break SHA-1.

How Hashing Works — interactive 3D animation

Step 01 of 09

1 · Any size in, 32 bytes out

This box runs SHA-256, the hash function behind download checksums, TLS certificates and Bitcoin. Feed it two letters, "hi", and the plate on the lid re-rolls to their hash: 32 tiles, one byte each, 64 hex characters in all. Now feed it a whole sentence, 43 bytes. The plate re-rolls again, and it is still 32 bytes. A password, a photo or a 4 GB film: the answer, called the hash or digest, is always exactly 32 bytes.

Step 02 of 09

2 · One letter changes everything

Hash the same sentence a million times and you get the same 32 bytes a million times. That is what lets a hash work as a fingerprint. Now change one letter: dog becomes cog. It is not one tile on the plate that changes. All 32 do. 123 of the 256 bits flipped, close to the half you would get by tossing a coin for each one. Nothing in the new digest hints at how similar the two sentences were.

Step 03 of 09

3 · First, fill a 64-byte block

Take the lid off. SHA-256 works on blocks of exactly 64 bytes, so the sentence is laid into a grid, two 4-byte words to a row. The 43 letters fill most of it. Then comes padding: a single 0x80 byte marks where the message ends, zeros fill the gap, and the last 8 bytes hold the message length in bits, 344. Because the length is written in, two different messages never pad out to the same bytes.

Step 04 of 09

4 · 64 rounds of mixing

Eight registers, A to H, hold 4 bytes each. They start from fixed values anyone can check: the fractional parts of the square roots of the first eight primes. Each round, the mixer takes one word from the message schedule and stirs it together with the registers and a round constant, using rotations, bit logic and addition. It writes a new A and a new E. Everything else shifts one place along, and H drops off the end. The first 16 words of the schedule are the block itself; the other 48 are mixed from those. Then it goes again, 64 times.

Step 05 of 09

5 · Add it back, and the way back is gone

After round 64 the registers drop onto the values they started from and are added in, 4 bytes at a time. That sum is the digest, and it rises out of the box. Every addition wraps round at 2³² and throws its carry away. A 64-byte block went in and only 32 bytes came out, so running the machine backwards would need numbers it never kept. The only way back is to guess inputs and compare, and nobody has found a shortcut.

Step 06 of 09

6 · Longer inputs, block after block

A 66-byte sentence does not fit in one block, so it is cut into two. Block 1 runs its 64 rounds and adds into the starting values, but that result does not leave the box. It becomes the starting values for block 2, which mixes on top of it. A 4 GB film is about 67 million blocks, each stirred into everything before it, and the digest comes out only after the last one. Change one byte anywhere and every block after it carries the difference.

Step 07 of 09

7 · The famous family

Here is the same sentence through four famous hash functions. MD5 (1992) gives 16 bytes and SHA-1 (1995) gives 20. SHA-256 (2001) and SHA3-256 (2015) give 32. More bytes means more possible answers, but length alone does not make a hash safe. MD5 and SHA-1 are broken. SHA-256 is not. SHA-3 is built on a completely different design, a "sponge", picked in 2012 so the world has a spare if SHA-2 ever falls.

Step 08 of 09

8 · What "broken" means

Broken means someone found a collision: two different inputs with the same digest. In 2017 researchers at CWI Amsterdam and Google made these two PDF files. They are different files, yet SHA-1 gives both exactly 38762cf7…. Finding them took 9,223,372,036,854,775,808 SHA-1 computations, about 6,500 CPU-years plus 110 GPU-years. Watch the rack as the input switches: MD5, SHA-256 and SHA3-256 all tell the two files apart. MD5 has its own collisions, though, and today one takes about a second on an ordinary PC.

Step 09 of 09

9 · Same in, same out. Never back.

That is how your laptop checks a 4 GB download against a 64-character code, how a website can store a hash of your password instead of the password itself, and how Git names every version of your code (classically with SHA-1, now moving to SHA-256). All of it rests on three facts. The same input always gives the same digest. A tiny change gives a completely different one. And there is no way back.