TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Hashing vs encryption

๐Ÿฅฉ Hashing vs encryption: cheat sheet

๐ŸŽจ The analogyA meat grinder versus a locked suitcase: mince never turns back into steak, but the suitcase opens with its key.
๐ŸŽ“ The regular explanationHashing is a one-way, fixed-size fingerprint of data, used for integrity checks and (slowly, with salt) for passwords; encryption is reversible with a key, used to keep data secret. HMACs and signatures add proof of who sent it, and Base64 is neither.
One-way hashing, two-way encryptionStep 4 of 4
Patties9f8key sha256(data) = 9f86d0โ€ฆ: 9f86d0012๐Ÿง‘โ€๐ŸณCookโš™๏ธMeat grinderone-way๐ŸงณLocked suitcaselocked๐Ÿ™‹Friend with the keyunlocked
  1. The cook puts a steak in the grinder and out comes a patty of mince. The same steak always makes exactly this patty.
  2. Try to turn the patty back into the steak. It can't be done, so the grinder only goes one way.
  3. The cook packs something into the suitcase and locks it. From the outside, nobody can see what's in it.
  4. The friend with the key opens the suitcase and gets back exactly what was packed.
Checking a delivery with a hashStep 4 of 4
๐Ÿ”ชButchertrusted source๐ŸššDelivery vanswapped๐Ÿฝ๏ธRestaurant3c1a7e โœ—
  1. The butcher grinds the steak and pins a photo of the patty on the shop's noticeboard for everyone to see.
  2. The van brings the steak. The restaurant grinds it, and the patty matches the photo, so nothing was swapped.
  3. Next time the van driver swaps in a different steak. The restaurant grinds it and the patty looks nothing like the photo, so it's sent back.
  4. The check only works because the photo came straight from the butcher. If the driver could swap the photo too, it would prove nothing.
Storing passwordsStep 5 of 5
Plain patty fridge ยท same patty!0b1key ana = sha256(password1): 0b14d500b1key ben = sha256(password1): 0b14d512Spiced patty fridge ยท all differentk9Fkey ana = argon2id(salt k9Fq, password1): k9Fq0Qz1key ben = argon2id(salt Qz1w, password1): Qz1w12๐Ÿ™‹Ana and Ben๐Ÿง‘โ€๐ŸณChef๐ŸฆนThiefyears per guess list
  1. Ana signs up with her secret steak. The chef runs it through a fast grinder and keeps only the patty in the fridge.
  2. Ben happens to own the very same steak, so his patty is identical to Ana's. Anyone looking at the fridge can see they match.
  3. A thief steals the fridge. With a book of patties from every common steak, already ground, he finds a match for both in seconds.
  4. The chef switches to a slow hand grinder and adds a pinch of each person's own spice, written on the label. Now Ana's and Ben's patties differ.
  5. The thief steals this fridge too, but his book is useless. He must crank the slow grinder for every guess, once for each person.
HMAC proves who sent itStep 3 of 3
๐Ÿ”ชButchershared spice๐ŸฆนCrook on the roadswapped steak๐Ÿฝ๏ธRestaurantpatty โœ—
  1. The butcher and the restaurant share a spice nobody else has. The butcher grinds the steak with it and tapes the spiced patty to the box.
  2. The restaurant grinds the steak with the same spice. The patties match, so it's untouched and really from the butcher.
  3. The crook swaps in a cheaper steak and grinds a new patty, but without the spice it can't match. The restaurant refuses it.
One key, a seal and a fresh dial numberStep 4 of 4
Luggage room ยท dial reused!c1key nonce n1 + ciphertext c1 + tag: c10c2key nonce n1 again (reused!) + ciphertext c2: c2โœฆโœฆโœฆโœฆโœฆ12๐Ÿง‘โ€๐ŸณYousame key๐Ÿ™‹Friendseal โœ— refused
  1. You and your friend hold copies of the same key. You set the dial to a fresh number, lock the suitcase, add a tamper seal and leave it in the luggage room.
  2. Your friend fetches it, sees the seal is whole and opens it with the same key.
  3. Someone in the luggage room tampers with the suitcase. Your friend sees the broken seal and refuses what's inside.
  4. You lock a second suitcase but forget to change the dial. With two suitcases on the same number, a snoop can learn what's in both and fake a seal.
Padlocks and sealsStep 4 of 4
๐Ÿง‘โ€๐ŸณAnaseal โœ“ Bob๐Ÿ•ต๏ธNosy portercan't open๐Ÿ™‹Bobopened โœ“
  1. Bob keeps his only key and hands out open padlocks to anyone who asks, including the porter.
  2. Ana snaps a padlock shut on her suitcase and sends it. The porter carries it but can't open it, even though he has padlocks too.
  3. Only Bob's own key opens the padlock, so Bob opens the suitcase.
  4. Bob replies with a note pressed with his wax seal. Ana checks it against the seal picture Bob handed out with his padlocks, so she knows it's his and untouched.

๐Ÿงฉ Key questions

The kitchen must keep a customer's card and later hand it to the bank, which must read it. Grinder or suitcase?
The suitcase, because the delivery firm needs the real thing back with the key. Mince never turns back into steak, and wrapping paper hides nothing. Only the suitcase gives the original back.
Ana and Ben own the same steak. Why should the chef add each person's own spice before grinding?
So their patties differ and a thief's book of pre-ground patties is useless. The spice is written on the label, so it isn't secret. It just makes every patty unique.
Why does the chef use a slow hand grinder for regulars' steaks instead of the fast electric one?
One slow check is fine at the door, but a thief's millions of guesses take far too long. Speed helps the thief, not you. Making each turn costly hurts him millions of times over.
A crook on the road swaps the steak and grinds a fresh patty to tape on the box. What stops him?
A spice only the butcher and restaurant share, which the crook can't add. Anyone can grind a plain patty, even a bigger one, but only the two of them can make the spiced one.
You lock two suitcases with the same key and the same dial number. What goes wrong?
A snoop comparing the two can learn a lot about what's inside, and fake a seal. The dial must be a fresh number every single time you lock with that key.