Rainbow tables
How a precomputed table of hash chains trades memory for time to reverse password hashes, worked through on 4-digit PINs, and why a per-user salt makes the whole trade worthless.
In 30 seconds
Sites store a hash of each password, not the password. A hash cannot be run backwards, so reversing one means guessing: slow if done afresh every time, huge if every answer is stored.
A rainbow table is the middle road. It links passwords and hashes into long chains and stores only each chain’s two ends; the middle is recomputed on demand.
It only works when everyone’s password is hashed the same way. A random salt per user, plus a deliberately slow hash, removes the shortcut.
Rainbow tables, step by step
Toy scale: 4-digit PINs and a hash cut to 4 hex digits. Real tables cover billions of passwords with chains thousands of links long, and lookups can hit false alarms that must be checked.
- Step 1: Sites store hashes. A careful site never keeps your password. It keeps a hash: the output of a one-way function H. Checking a login is easy (hash what was typed, compare), but a stolen hash cannot simply be run backwards to get the password.
- Step 2: Two blunt ways back. To reverse a hash you can only guess, hash and compare. Guessing afresh for every hash costs time. Hashing every possible password once and storing all the answers costs memory. A rainbow table sits between: it stores a little and recomputes the rest.
- Step 3: A reduction function. The trick needs a second function, R, that turns any hash into some password from the list of candidates. It does not undo H. It is simply a fixed, repeatable way to get from a hash to another password worth trying.
- Step 4: Build a chain. Start from any password and alternate: hash, reduce, hash, reduce. Then throw the middle away and keep only the two ends. The chain can be replayed from its start whenever needed, so one stored pair stands for every password along it.
- Step 5: A table of chains. Repeat from many starting points and the table is just a list of starts and ends. Each column of a chain uses its own reduction (R1, R2, R3); colour each column differently and you get the rainbow of the name. That keeps two chains that collide in different columns from merging.
- Step 6: Look up a stolen hash. Take the stolen hash and finish the chain it might sit in, guessing its column: last, then one earlier, and so on. When the result equals a stored END, replay that row from its START. The password just before the matching hash is the answer.
- Step 7: Why a salt defeats it. A salt is a random value, different for each user, stored beside the hash and mixed into it. The same password now gives a different hash for every user, so a table built in advance matches none of them. Add a deliberately slow hash (bcrypt, scrypt, Argon2) and every guess costs real time.
Try it yourself
Learn this first
Spotted a mistake on this page?