Encoding, encryption, hashing, signing: what's the difference?
Encoding changes format and protects nothing. Encryption hides data from anyone without the key and can be reversed. Hashing can't be reversed and proves data hasn't changed. Signatures and MACs prove who produced the data. Which one to use for passwords, secrets, webhooks, tokens and files, with…
The verdict, which each case below will confirm: they answer different questions.
| Question it answers | Reversible? | Needs a key? | Examples | |
|---|---|---|---|---|
| Encoding | “How do I represent these bytes as text?” | Yes, by anyone | No | Base64, hex, URL encoding, UTF-8 |
| Encryption | “How do I keep this secret?” | Yes, with the key | Yes | AES-256-GCM, ChaCha20-Poly1305 (symmetric); RSA-OAEP, HPKE with X25519 (public key) |
| Hashing | “Has this changed? Is this the same thing?” | No | No | SHA-256, SHA-3, BLAKE3 |
| Password hashing | “Is this the right password, without storing it?” | No, and deliberately slow | No (salt, optionally a pepper) | argon2id, scrypt, bcrypt |
| MAC | “Did someone who knows our shared key produce this?” | No | Yes, shared | HMAC-SHA256 |
| Signature | “Did the holder of this private key produce this?” Anyone can check | No | Private key signs, public key verifies | Ed25519, ECDSA P-256, RSA-PSS |
The most common culprit is treating the first row as if it were the second: Base64 is not encryption. A JWT’s payload, a Kubernetes secret’s value and a Basic auth header are all Base64; anyone can read them. That string of gibberish looks protected, which is exactly why it fools people.
These four words get muddled constantly, and the muddle is where leaks begin. That’s a shame, because each one is easy to choose once you see the question it answers. Let’s take the cases one at a time.
Diagram source
flowchart LR
D[Data] -->|encode| E1[Same data, different format<br/>anyone can decode]
D -->|encrypt with key| E2[Ciphertext<br/>only key holders can decrypt]
D -->|hash| E3[Fixed-size fingerprint<br/>can't go back]
D -->|MAC with shared key| E4[Data + tag<br/>key holders can verify origin]
D -->|sign with private key| E5[Data + signature<br/>anyone with public key can verify]
Case file
The case A string of gibberish looks protected, and leaks Suspects Encoding, encryption, hashing, signing, all muddled together Culprit Treating Base64 as if it were encryption The fix Pick by the question: format, secrecy, integrity or origin Evidence The blogging platform: argon2id for passwords, AES-256-GCM for stored secrets
Case 1: storing passwords #
Passwords are where the “deliberately slow” idea pays off, and it’s a lovely bit of asymmetry: you make each guess expensive for the attacker while one honest login barely notices.
Use: a password hashing function: argon2id (or scrypt or bcrypt), with a unique random salt per password, tuned to take tens of milliseconds.
The false leads: encryption (whoever has the key can read every password), or a fast hash like SHA-256 (attackers can try billions of guesses per second on GPUs). The password storage post has the parameters. The blogging platform I run uses argon2id with 64 MiB of memory per hash.
Case 2: secrets your app must read back #
API keys for third-party services, 2FA secrets, OAuth tokens you store for users: your app needs the original value, so hashing won’t work.
Use: authenticated encryption, AES-256-GCM or XChaCha20-Poly1305, with a key kept outside the database (an environment variable, a secrets manager or a KMS). The blogging platform encrypts email-provider keys and TOTP secrets this way with AES-256-GCM and a key from the environment, so a database dump alone reveals nothing.
“Authenticated” is the key clue: the same operation that hides the data also detects tampering. Two guarantees for the price of one. Never use encryption without authentication (AES-CBC alone, AES-ECB ever).
Case 3: webhooks #
A payment provider calls your endpoint saying “payment succeeded”. How do you know it’s really them, and not an impostor who found your URL?
Use: a MAC. You and the provider share a secret; they send HMAC-SHA256(secret, timestamp + body) in a header. You compute the same and compare with a constant-time comparison, and reject old timestamps to stop replays.
Some providers use signatures instead (Ed25519 or RSA). Then they keep the private key and you only need their public key, so a leak of your configuration can’t be used to forge their messages. That asymmetry, the idea Diffie and Hellman opened up in the 1970s, still feels a little like a conjuring trick to me.
Case 4: tokens #
- Session tokens (random strings): store only a hash (SHA-256) of each in the database. If the table leaks, the hashes can’t be used as cookies. Fast hashing is fine here because the token is 256 random bits, not a guessable password.
- JWTs are signed, not encrypted: anyone holding one can read the claims (the sessions or JWTs post compares the two). Don’t put secrets or personal data you wouldn’t show the user inside. Verify the algorithm you expect (
algpinned), the signature, expiry and audience. - Encrypted tokens (JWE, or PASETO’s
localtokens) exist for when the client mustn’t read the contents; they’re rarely needed.
Case 5: files and downloads #
- Checksums (SHA-256) published next to a download prove the file wasn’t corrupted, but only if you get the checksum from a trusted place.
- Signatures (Sigstore/cosign for containers, GPG or minisign for releases) prove who built it.
- Content addressing (Git, container image digests) names data by its hash, so the name itself verifies the content. The name is the proof, which I find one of the neatest ideas in all of computing.
Remember this
- Encoding is for transport, never for protection.
- Passwords: slow hashes. Random tokens: fast hashes. Data you need back: authenticated encryption.
- Shared secret: HMAC. Anyone must verify: signatures.
Case closed: rules of thumb #
- Never invent crypto, and never combine primitives yourself if a library offers the combination (use libsodium/NaCl, Go’s
crypto, Tink). - Encoding is for transport, never for protection.
- Passwords: slow password hashes. Random tokens: fast hashes. Data you need back: authenticated encryption.
- Integrity between two parties who share a secret: HMAC. Integrity anyone must verify: signatures.
- Keys live outside the data they protect, and you can rotate them.
Once the four questions are clear, the mystery dissolves and the choices mostly make themselves. And the tools for answering them are free, well tested and sitting in your standard library, thanks to decades of careful work by cryptographers.
Comments
No comments yet.
Sign in to comment. We'll email you a link; no password needed.