The Plain Picture#
Three very different things people constantly mix up:
Writing a letter in a different alphabet (Roman → Devanagari). Anyone who knows both alphabets can read it. Not secret.
A fingerprint. It identifies a person perfectly, but you can't rebuild the person from it.
A locked box. Only someone with the key can open it and read the letter.
The Real Names#
| Encoding | Hashing | Encryption | |
|---|---|---|---|
| Picture | different alphabet | fingerprint | locked box |
| Purpose | represent data in a different format | fixed-size fingerprint of data | keep data secret |
| Reversible? | yes, by anyone | no (one-way) | yes, with the key |
| Needs a key? | no | no | yes |
| Examples | Base64, URL-encoding, UTF-8 | SHA-256 | AES, RSA |
| Use for | sending binary data as text | integrity checks, password storage (Ch 3), Git (Git Ch 3) | secret data |
Encoding — Base64#
Base64 turns any bytes into text using 64 safe characters (A–Z, a–z, 0–9, +, /). It's used to put binary data inside
text formats like JSON, email and HTTP headers. It provides zero secrecy:
$ echo -n "Priya:s3cret" | base64
UHJpeWE6czNjcmV0
$ echo "UHJpeWE6czNjcmV0" | base64 -d
Priya:s3cretHTTP "Basic" authentication sends exactly this in a header. That's why it's only acceptable over HTTPS.
Hashing — real properties you can see#
A cryptographic hash function like SHA-256 turns any input into a 256-bit (64 hex character) fingerprint:
$ echo -n "hello" | shasum -a 256
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
$ echo -n "hellp" | shasum -a 256
fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7The four properties that make it useful:
Deterministic — the same input always gives the same hash.
One-way — you can't work backwards from the hash to the input.
Avalanche effect — changing one letter (
o→p) changes the output completely, as above.Collision-resistant — it's practically impossible to find two different inputs with the same hash.
Uses: checking a downloaded file wasn't corrupted or tampered with (compare its hash to the published one); Git's object names; detecting duplicate files; and — with extra care — passwords (Ch 3).
Broken hashes: MD5 and SHA-1 are broken for security — researchers can create collisions. Don't use them for anything security-related. Use SHA-256 or newer.
Gotchas
"We encrypted the passwords with Base64" is a real thing people have said. Base64 is encoding. Anyone can reverse it.
Hashing is not encryption — there's no key and no way back. If you need to read the data again later, you need encryption.
Check yourself
4 questionsWhich of encoding, hashing and encryption can be reversed, and by whom?
Show answerHide answer
Encoding: by anyone. Encryption: by whoever has the key. Hashing: by no one — it's one-way.
Name the four properties of a cryptographic hash.
Show answerHide answer
Deterministic, one-way, avalanche effect, collision-resistant.
Why is Base64 not a security measure?
Show answerHide answer
It's a public, keyless format conversion; anyone can decode it.
Which hash functions should you no longer use for security?
Show answerHide answer
MD5 and SHA-1.