Guide

Encoding, hashing and encryption are three different things

Published · 8 minute read

These three words are used interchangeably in bug reports and, less forgivably, in code. They solve different problems, and choosing the wrong one produces systems that look protected and are not. The quickest way to tell them apart is to ask two questions: can it be reversed, and does reversing it need a secret?

Reversible?Needs a secret?Purpose
EncodingYes, by anyoneNoRepresent data in a form a system can carry
HashingNoNoProduce a fixed-size fingerprint of data
EncryptionYes, with the keyYesKeep data secret from anyone without the key
SigningNot applicableYesProve who produced data and that it is unchanged

Encoding: changing the alphabet

Encoding rewrites data using a different set of symbols so that it survives a channel that would otherwise damage it. There is no key and no secret. Anyone can decode it, and that is the intention.

The mistake to avoid is treating an unfamiliar-looking string as protected. A Base64 string in a config file, a cookie or an HTTP Basic Authorization header is plain text to anyone who pastes it into a decoder. Kubernetes "secrets" are Base64-encoded by default for exactly the transport reason above, and that alone protects nothing.

A second, quieter mistake is encoding twice. A value that is URL-encoded and then encoded again turns %20 into %2520. If you see %25 in a URL where you did not expect a literal percent sign, something was encoded twice.

Hashing: a one-way fingerprint

A cryptographic hash function takes input of any size and returns a fixed-size value: 256 bits for SHA-256. The same input always gives the same output, a one-character change in the input changes the output completely, and there is no practical way to work backwards from the output to the input.

Hashes are for comparison without disclosure:

Two points catch people out.

A hash is not secret-keeping for small inputs. You cannot reverse SHA-256, but you can guess. If the input is a phone number or a six-digit code, an attacker hashes every possibility in seconds and looks up the match. Hashing only hides inputs that are unpredictable.

MD5 and SHA-1 are broken for security use. Researchers can construct two different files with the same MD5 or SHA-1 hash. They remain acceptable for catching accidental corruption, but nothing that must resist a deliberate attacker should rely on them. Use SHA-256 or newer. You can compare the algorithms side by side in the Hash Generator.

Passwords are a special case

Passwords should be hashed, never encrypted, so that even the service cannot recover them. But a general-purpose hash such as SHA-256 is the wrong choice, because it is designed to be fast, and speed is exactly what an attacker with a stolen database wants. Modern hardware tries billions of SHA-256 guesses per second.

Use a function designed for passwords: Argon2id, scrypt or bcrypt. They differ from plain hashes in two ways:

Use the implementation from your framework or a well-known library. It generates the salt and stores the parameters in the output for you.

Encryption: secrecy with a key

Encryption transforms data so that it can only be read by someone holding the right key. It comes in two forms.

Encryption alone hides content but does not prevent tampering: with some modes an attacker can alter encrypted data in predictable ways without knowing the key. Use an authenticated mode such as AES-GCM or ChaCha20-Poly1305, which detects any change, and never reuse a nonce with the same key. Better still, use a high-level library that makes those choices for you rather than assembling primitives yourself.

Signing: proof of origin

A signature answers a different question from encryption: not "who can read this?" but "who wrote this, and has it been altered?". An HMAC combines a hash with a shared secret; anyone with the secret can both create and check it. A digital signature is made with a private key and checked with the public key, so many parties can verify it while only one can produce it.

Webhook signatures, signed cookies and JWTs all use one of these. Note that a signed JWT is readable by anyone: signing gives integrity, not secrecy.

Which one do I need?

You want to…Use
Put binary data in JSON, a URL or an emailEncoding (Base64, base64url)
Put arbitrary text in a query stringURL encoding
Check a file was not corrupted or changedA hash (SHA-256)
Store user passwordsA password hash (Argon2id, bcrypt)
Store data you must read back later, such as an API key for another serviceEncryption (AES-GCM), with the key kept elsewhere
Prove a message came from you and was not alteredHMAC or a digital signature
Protect data in transitTLS, which combines all of the above

Mistakes worth checking your own code for

Tools used in this guide

More guides