Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    Hashing, and Its Two Look-Alikes

    "We encrypted the passwords with Base64" is a real thing people have said. Base64 is encoding. Anyone can reverse it.

    Chapter 2 of 134 min read

    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#

    EncodingHashingEncryption
    Picturedifferent alphabetfingerprintlocked box
    Purposerepresent data in a different formatfixed-size fingerprint of datakeep data secret
    Reversible?yes, by anyoneno (one-way)yes, with the key
    Needs a key?nonoyes
    ExamplesBase64, URL-encoding, UTF-8SHA-256AES, RSA
    Use forsending binary data as textintegrity 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:

    Output
    $ echo -n "Priya:s3cret" | base64
    UHJpeWE6czNjcmV0
    $ echo "UHJpeWE6czNjcmV0" | base64 -d
    Priya:s3cret

    HTTP "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:

    Output
    $ echo -n "hello" | shasum -a 256
    2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
    $ echo -n "hellp" | shasum -a 256
    fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7

    The four properties that make it useful:

    1. Deterministic — the same input always gives the same hash.

    2. One-way — you can't work backwards from the hash to the input.

    3. Avalanche effect — changing one letter (o → p) changes the output completely, as above.

    4. 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 questions
    1

    Which of encoding, hashing and encryption can be reversed, and by whom?

    Show answer

    Encoding: by anyone. Encryption: by whoever has the key. Hashing: by no one — it's one-way.

    2

    Name the four properties of a cryptographic hash.

    Show answer

    Deterministic, one-way, avalanche effect, collision-resistant.

    3

    Why is Base64 not a security measure?

    Show answer

    It's a public, keyless format conversion; anyone can decode it.

    4

    Which hash functions should you no longer use for security?

    Show answer

    MD5 and SHA-1.