Encoding, hashing and encryption are three different things
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 | |
|---|---|---|---|
| Encoding | Yes, by anyone | No | Represent data in a form a system can carry |
| Hashing | No | No | Produce a fixed-size fingerprint of data |
| Encryption | Yes, with the key | Yes | Keep data secret from anyone without the key |
| Signing | Not applicable | Yes | Prove 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.
- Base64 turns arbitrary bytes into 64 safe text characters, so that binary data can travel through systems built for text: email, JSON, data URLs. Every 3 bytes become 4 characters, so the result is about 33% larger. The
=signs at the end are padding. - Base64url is the same idea with
-and_in place of+and/, so the result can sit in a URL or a file name. JWTs use it. - URL encoding (percent-encoding) replaces characters that have a special meaning in a URL, such as a space,
&or?, with%20,%26,%3F. - Hex writes each byte as two characters from 0–9 and a–f.
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:
- Checking that a downloaded file matches the one the publisher released.
- Detecting whether content changed, as Git does for every object it stores.
- Building keys for caches and for deduplicating identical content.
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:
- They are deliberately slow and, for Argon2 and scrypt, memory-hungry, with a cost setting you raise as hardware improves.
- They mix in a salt, a random value stored next to each hash, so that two users with the same password have different hashes and precomputed tables are useless.
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.
- Symmetric encryption uses one key to encrypt and decrypt. AES is the standard. It is fast and suited to data at rest and to bulk traffic. The difficulty is getting the key to the other party safely.
- Asymmetric encryption uses a pair of keys: a public one that anyone may use to encrypt, and a private one that decrypts. RSA and elliptic-curve schemes work this way. They are slow, so in practice they are used to agree on a symmetric key, which then does the real work. TLS does exactly this.
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 email | Encoding (Base64, base64url) |
| Put arbitrary text in a query string | URL encoding |
| Check a file was not corrupted or changed | A hash (SHA-256) |
| Store user passwords | A password hash (Argon2id, bcrypt) |
| Store data you must read back later, such as an API key for another service | Encryption (AES-GCM), with the key kept elsewhere |
| Prove a message came from you and was not altered | HMAC or a digital signature |
| Protect data in transit | TLS, which combines all of the above |
Mistakes worth checking your own code for
- Calling Base64 or hex "encryption" in comments, variable names or documentation. The next reader will believe it.
- Storing passwords with MD5, SHA-1 or unsalted SHA-256.
- Hashing low-entropy values such as emails or phone numbers and calling the result "anonymised".
- Comparing secrets or signatures with an ordinary
==. Use a constant-time comparison so response timing does not leak how many leading characters were right. - Encrypting data and storing the key in the same database table, file or repository.
- Inventing a scheme. The value of standard algorithms is the years of public attack they have survived.