Guide
Hashing vs encryption vs encoding
Three ways to transform data that look alike, a jumble of letters and digits, but do completely different jobs. Mixing them up is behind a long list of real security failures.
By Mark Stoumann ·
The short version
| Encoding | Hashing | Encryption | |
|---|---|---|---|
| Purpose | Make data fit a format | Fingerprint data | Keep data secret |
| Reversible? | Yes, by anyone | No | Yes, with the key |
| Needs a key? | No | No (HMAC adds one) | Yes |
| Output size | Grows with the input | Fixed (256 bits for SHA-256) | About the input’s size |
| Examples | Base64, URL encoding, UTF-8, hex | SHA-256, SHA-3, MD5 | AES-GCM, ChaCha20-Poly1305, RSA |
One question sorts almost every case: should anyone be able to get the original back? If everyone may, encode. If no one needs to, hash. If only someone with a key may, encrypt.
Encoding: a change of format, not a secret
Encoding rewrites data so it survives a particular channel. Base64 turns arbitrary bytes into 64 safe characters for email, JSON and data URLs. Percent-encoding makes text safe inside a URL. UTF-8 turns characters into bytes. The rules are public and there is no key, so decoding is always one function call away:
btoa('hunter2') // 'aHVudGVyMg=='
atob('aHVudGVyMg==') // 'hunter2'That makes Base64 strings look scrambled while hiding nothing. Common places this bites:
- HTTP Basic authentication sends
user:passwordBase64-encoded. Without HTTPS, anyone on the network can read it. - Kubernetes Secrets store values Base64-encoded in their YAML. Committing one to a repository commits the secret.
- A JWT payload is Base64url-encoded JSON, readable by anyone who holds the token, as the JWT decoder shows.
If a value looks like Base64, the Base64 decoder will tell you in a second what it actually says.
Hashing: a one-way fingerprint
A cryptographic hash takes any input and returns a fixed-size digest. The same input always gives the same digest, a tiny change gives a completely different one, and there is no function that goes back:
sha256('hunter2')
// f52fbd32b2b3b86ff88ef6c490628285f482af15ddcb29541f94bcf526a3f6c7A secure hash must resist three things: finding an input for a given digest (preimage), finding a second input with the same digest as a known one (second preimage), and finding any two inputs that share a digest (a collision). Collisions are the easiest of the three to find, and they are where MD5 and SHA-1 fell.
How MD5 and SHA-1 were broken
A broken hash doesn’t mean someone can reverse it. It means attackers can craft two different inputs with the same digest, and anything that trusts the digest to stand for one specific document, such as a signature, can then be fooled.
- MD5, 2004: Xiaoyun Wang presented the first MD5 collision at the Crypto conference.
- MD5, 2008: a team including Marc Stevens and Arjen Lenstra used a cluster of more than 200 PlayStation 3 consoles to build a rogue certificate authority certificate that every common browser trusted, by exploiting a CA that still signed with MD5.
- SHA-1, 2017: CWI Amsterdam and Google published SHAttered, the first SHA-1 collision: two different PDF files with the same hash, after about 6,500 CPU-years and 110 GPU-years of computation.
- SHA-1, 2020: Gaëtan Leurent and Thomas Peyrin produced a chosen-prefix collision, the more dangerous kind, on 900 rented gaming GPUs, estimating the cost at about US$45,000, and used it to forge a PGP key signature.
NIST now recommends phasing SHA-1 out completely by the end of 2030. For new work, use SHA-256 or another member of the SHA-2 or SHA-3 families. MD5 is still fine where nobody is trying to cheat, such as spotting a corrupted download or deduplicating files in a cache.
Hashing with a key: HMAC
A plain hash proves nothing about who made it: anyone can hash a tampered message too. An HMAC mixes a secret key into the hash, so only someone with the key can produce the right value. GitHub, for example, signs each webhook delivery with HMAC-SHA256 and sends the result in an X-Hub-Signature-256 header. Checking it in Node:
import { createHmac, timingSafeEqual } from 'node:crypto'
const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex')
const received = request.headers['x-hub-signature-256'] ?? ''
const valid =
received.length === expected.length &&
timingSafeEqual(Buffer.from(received), Buffer.from(expected)) Two details matter. Hash the raw request body, not JSON you parsed and stringified again, since any change in spacing changes the digest. And compare in constant time: GitHub’s docs warn never to use a plain ==, which can leak through timing how many leading characters matched. HS256 JWTs are signed the same way; the JWT best practices guide covers how long that key must be.
Passwords: hash, but slowly
Passwords are the one case where a good general-purpose hash is the wrong choice. SHA-256 is designed to be fast, and fast is exactly what an attacker with a leaked database wants: they hash billions of likely passwords per second and look for matches. A salt (a random value stored with each hash) stops them cracking every user at once, but not from trying the common passwords against each one.
Password hashes are deliberately slow and, in the better ones, memory-hungry. OWASP’s current minimums:
| Algorithm | OWASP minimum settings |
|---|---|
| Argon2id (first choice) | 19 MiB of memory, 2 iterations, parallelism 1 |
| scrypt | N = 217 (128 MiB), r = 8, p = 1 |
| bcrypt | Work factor 10 or more; inputs limited to 72 bytes |
| PBKDF2-HMAC-SHA256 | 600,000 iterations |
OWASP is explicit that passwords should be hashed, not encrypted: an encrypted password table is one stolen key away from a full leak. The rare exception is when your app must replay the password to another system, and then it is really a stored credential, not a login check. The hash generator on this site makes fast hashes for checksums and comparisons; it is not a password hasher.
Encryption: secret, and reversible
Encryption is what you need when the data must be read again, but only by the right people: database fields with personal data, backups, files sent between services. Use an authenticated mode such as AES-GCM, which detects tampering as well as hiding content. In the browser and in Node, the Web Crypto API does it without any library:
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt'])
const iv = crypto.getRandomValues(new Uint8Array(12)) // new for every message
const ciphertext = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv }, key, new TextEncoder().encode('hunter2'))The ciphertext is 16 bytes longer than the input: that is the authentication tag. Store the IV next to it (it isn’t secret) and never reuse an IV with the same key. The hard part of encryption is rarely the algorithm; it is keeping the key somewhere safer than the data.
Quick answers to common mix-ups
- “We hash the email addresses, so they’re anonymous.” Not really. There are only so many email addresses and phone numbers, and anyone can hash a list of them and match. Hashed personal data is still personal data.
- “The API key is encrypted with Base64.” It is encoded. Treat it as plain text.
- “We encrypt passwords with SHA-256.” SHA-256 hashes; it doesn’t encrypt. And for passwords, use Argon2id or bcrypt instead.
- “Hex output means it’s a hash.” Hex is just another encoding. A 64-character hex string could be a SHA-256 digest, 32 random bytes or an encoded key.
Frequently asked questions
Is Base64 a form of encryption?
No. Base64 is an encoding: a public, keyless way to write bytes as text. Anyone can decode it instantly, so Base64 offers no secrecy at all. Credentials in a Basic Authorization header, for example, are only Base64-encoded and need HTTPS to stay private.
Can a hash be reversed?
Not mathematically, but it can often be guessed. A fast hash of a short or common input, such as a password or a phone number, can be found by hashing candidates until one matches. That is why passwords need a slow, salted password hash such as Argon2id or bcrypt.
What is the difference between hashing and encryption?
Encryption is reversible with the right key and is used to keep data secret while it is stored or sent. Hashing is one-way and keyless: it produces a fixed-size fingerprint used to check integrity or to compare values without storing them.
Is SHA-256 still secure?
Yes. No practical collision or preimage attack on SHA-256 is known, and NIST recommends moving from SHA-1 to the SHA-2 and SHA-3 families. It is the wrong tool only for passwords, where its speed works against you.
What is HMAC used for?
HMAC combines a hash with a secret key, so only someone with the key can produce a valid value. It proves that a message came from someone who knows the key and was not changed, which is how services like GitHub sign webhook deliveries and how HS256 JWTs are signed.