Encoding vs encryption vs hashing: the difference that matters
“We store the passwords in Base64, so they’re encrypted.” “The API key is hashed, so we can send it back to the user later.” Both sentences come from real projects, and both describe security holes. Encoding, encryption and hashing all turn data into something that looks unreadable, which is why they get mixed up — but they answer completely different questions. Choosing the wrong one is how databases full of “protected” passwords end up readable in plain text.
Encoding: changing the format, not hiding anything
Encoding transforms data so it can travel safely through a particular channel. The rules are public, there is no key, and anyone can reverse it. Its job is compatibility, not secrecy.
// Base64: binary-safe text
Buffer.from('p@ssw0rd').toString('base64'); // 'cEBzc3cwcmQ='
Buffer.from('cEBzc3cwcmQ=', 'base64').toString(); // 'p@ssw0rd'
// URL (percent) encoding: safe inside a query string
encodeURIComponent('a b&c=d'); // 'a%20b%26c%3Dd'
Other examples: UTF-8 (text to bytes), hex, HTML entities such as <. If you can decode it without a secret, it protects nothing. See Base64 explained for how the most common one works.
Encryption: hiding data from everyone without the key
Encryption turns plaintext into ciphertext that can only be turned back with the right key. It is reversible by design — for whoever holds the key. Use it when you need the original data again later: messages, files, database fields like national ID numbers, backups.
- Symmetric (AES): the same key encrypts and decrypts. Fast; used for data at rest and inside TLS.
- Asymmetric (RSA, elliptic-curve schemes): a public key encrypts (or verifies), a private key decrypts (or signs). Used to exchange keys and for digital signatures.
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
const key = randomBytes(32); // 256-bit key: keep it secret
const iv = randomBytes(12); // new random IV for every message
const cipher = createCipheriv('aes-256-gcm', key, iv);
const ciphertext = Buffer.concat([cipher.update('Meet at 5pm', 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();
const decipher = createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(tag);
Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8'); // 'Meet at 5pm'
AES-GCM also detects tampering: if one byte of the ciphertext changes, decipher.final() throws. The hard part of encryption is rarely the algorithm; it is key management — where the key lives, who can read it, and how it is rotated. A key hard-coded next to the data it protects is barely better than no encryption.
Hashing: a one-way fingerprint
A cryptographic hash function maps any input to a fixed-size output, and it is not reversible — there is no key and no “unhash”. The same input always gives the same hash, and a tiny change in the input changes the output completely:
sha256('hello') = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha256('Hello') = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
Hashes are used to check integrity (download checksums, Git commits), to deduplicate data, and as a building block for signatures. You check a hash by hashing the candidate again and comparing.
Side by side
| Encoding | Encryption | Hashing | |
|---|---|---|---|
| Purpose | Compatibility / transport | Confidentiality | Integrity / fingerprint |
| Reversible? | Yes, by anyone | Yes, with the key | No |
| Needs a key? | No | Yes | No (HMAC adds one) |
| Output size | Depends on input | About the input size | Fixed (e.g. 256 bits) |
| Examples | Base64, URL encoding, UTF-8 | AES-GCM, RSA-OAEP, ChaCha20-Poly1305 | SHA-256, SHA-3, BLAKE2 |
Passwords: slow, salted hashes only
Passwords should be hashed, never encrypted — the server only ever needs to check a login, not read the password. But a fast hash is the wrong tool. SHA-256 is designed to be quick, so a modern GPU can test billions of guesses per second against a leaked hash. MD5 and SHA-1 are even faster and have known weaknesses; unsalted MD5 hashes of common passwords can simply be looked up — search the web for 5d41402abc4b2a76b9719d911017c592 and you will find it is hello.
Use a dedicated password-hashing function: Argon2id (first choice for new systems), scrypt or bcrypt. They are deliberately slow and memory-hard, have a tunable cost, and take a random salt per user so that two people with the same password get different hashes and precomputed tables are useless.
import { scryptSync, randomBytes, timingSafeEqual } from 'node:crypto';
const PARAMS = { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 };
function hashPassword(password) {
const salt = randomBytes(16);
const hash = scryptSync(password, salt, 32, PARAMS);
return `${salt.toString('hex')}:${hash.toString('hex')}`;
}
function checkPassword(password, stored) {
const [saltHex, hashHex] = stored.split(':');
const hash = scryptSync(password, Buffer.from(saltHex, 'hex'), 32, PARAMS);
return timingSafeEqual(hash, Buffer.from(hashHex, 'hex'));
}
On a typical laptop each call takes a few hundred milliseconds — unnoticeable for one login, crippling for an attacker. In Python, the argon2-cffi and bcrypt packages do the same job and store the salt and cost inside the hash string for you. Note that bcrypt only uses the first 72 bytes of a password.
Never store passwords as Base64, with reversible encryption, or as plain MD5, SHA-1 or SHA-256 — salted or not.
HMAC: hashing with a key
An HMAC combines a hash with a secret key. Only someone with the key can produce the right value, so it proves a message came from a key holder and wasn’t changed. Payment gateways use it to sign webhooks; HS256 JWTs are HMAC-SHA256.
createHmac('sha256', 'my-secret-key').update('amount=500&to=rahim').digest('hex');
// '0c91fba74f9d3a7ec543c117d241883b51d6a9a3df886463b813c6cd94dccd3c'
Compare HMACs with a constant-time function such as timingSafeEqual or Python’s hmac.compare_digest, not ===.
Common misconceptions
- “It looks scrambled, so it’s secure.” Base64 and hex look scrambled too. Ask: can someone reverse it without a secret?
- “Hashing is encryption you can’t undo.” Close, but the key difference is intent: hashes are for comparing, encryption is for getting the data back.
- “A JWT is encrypted.” A normal signed JWT is only encoded and signed; anyone can read the payload.
- “SHA-256 with a salt is fine for passwords.” The salt stops lookup tables, but not billions of fast guesses. Speed is the problem.
FAQ
Can a hash be decrypted?
No. But a weak or fast hash of a short or common input can be guessed: an attacker hashes candidates until one matches. That is why passwords need slow, salted algorithms.
Should I encrypt or hash API keys I issue?
Hash them (a fast hash such as SHA-256 is fine here, because random keys are too long to guess) and show the full key to the user only once. If you must be able to display a secret again, such as a third-party key your app uses, encrypt it and protect the encryption key.
Is MD5 still useful for anything?
As a non-security checksum to spot accidental corruption, yes. For passwords, signatures or anything an attacker could influence, no — use SHA-256 or better.