Encryption vs Hashing vs Encoding — Security

Encoding (Base64) only changes the format of data: it's two-way, has no key, and is not security, because anyone can reverse it with atob. Hashing (SHA-256…

During a code review (checking a teammate's code before it is used), you find a line that stores users' passwords with btoa(password). btoa turns text into a run of letters and numbers that looks random. Its author is sure it's safe: "it's scrambled, nobody can read it". Yet anyone holding the database can turn every one of those passwords back with a single line of code.

That mix-up happens because encoding, hashing and encryption all turn data into some other form that you can't read right away. But their purposes are very different. Picture three things. Writing in Morse code can be translated back by anyone. A fingerprint can be matched against a person who arrives, but it can't be used to rebuild the person. A letter in a safe can only be opened by someone with the key. Encoding is the Morse code, hashing is the fingerprint, and encryption is the safe.

Encoding: a change of format, not security

Encoding changes the format of data so it can be sent or stored whole in a place that accepts only text, such as JSON or a URL. Anyone can encode, and anyone can decode. No key, no secret. The most common example is Base64:

const encoded = btoa("Hello");  // "SGVsbG8="
const decoded = atob("SGVsbG8="); // "Hello"

Base64 is not security. Encoding a password with Base64 is like writing it on paper and folding it: it looks different, but anyone can unfold it.

Hashing: one-way, for a fingerprint of data

Hashing turns any data into a string with a fixed length. The result, called a hash, can't be turned back into the original (one-way). The same input always produces the same hash. If you change just one letter, the whole hash changes completely. Here is an example with SHA-256:

// SHA-256 in the browser, through the Web Crypto API
const data = new TextEncoder().encode("Hello");
const buf = await crypto.subtle.digest("SHA-256", data);
const hex = [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join("");
// "185f8db32271fe25f561a6fc938b2e264306ec304eda5180..."

Hashing is used to check file integrity: you compare a file's hash (its checksum) to make sure the file hasn't changed. It's also used to compare two pieces of data without storing the originals. But SHA-256 is too fast for passwords: an attacker can try billions of guesses a second. For passwords, use a hash made for passwords, bcrypt or argon2. Both are made slow on purpose and use a salt.

Encryption: two-way, needs a key

Encryption turns data into ciphertext (encrypted data that looks random). Ciphertext can only be read again with the right key (two-way). Encryption keeps data secret when you need to read it again later. The example below uses AES-GCM, a symmetric encryption: the same single key locks and unlocks.

// Make a random key and a nonce (a use-once number)
const key = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]);
const iv = crypto.getRandomValues(new Uint8Array(12));
const message = new TextEncoder().encode("secret data");

const cipher = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, message);
const back = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, cipher);
// new TextDecoder().decode(back) === "secret data"

Asymmetric encryption (RSA, for example) uses two keys: a public key to lock, and a private key to unlock. In TLS, the server uses a pair of a public key and a private key to prove who it is through its certificate. The session key itself comes from a key exchange (ECDHE in TLS 1.3).

Picking the right one

Need Use Why
Storing a password hashing (bcrypt/argon2) no need to read it again, only to match it
Secret data you need to read again encryption (AES) can be decrypted with the key
Sending binary data through text encoding (Base64) only about format, not secrecy
Verifying file integrity hashing (SHA-256) a fingerprint that's fast to compare

For a password, the answer is hashing. Why not encryption? Because encryption can be decrypted: if the key leaks, all passwords are open at once. Hashing can't be reversed, so even if the database leaks, the real passwords stay hidden.

In the exercise, you write classifyTransform(name). It goes the other way from this table: the function takes a technique's name (base64, sha256, AES, bcrypt) and returns its group, "encoding", "hashing" or "encryption", ignoring upper and lower case. A name on none of the three lists gets "unknown".

💡 Certificate pinning

Related to TLS, there's an extra technique called certificate pinning. The app keeps (pins) the server's certificate (or its public key) inside its own code, then refuses a connection if the server's certificate doesn't match the pinned one. The goal is to defeat an eavesdropper using a rogue CA: a certificate issuer that is willing to make a fake certificate for your domain. Normally a browser trusts hundreds of CAs, so a fake certificate from any one of them is still accepted. With pinning, that fake certificate is refused, so the attacker still needs the right key, the one the real server holds. But OWASP now advises: don't pin except in a native mobile app, and even there only rarely. If you do pin, pin at least two public keys (a main one and a backup), so that a key rotation (replacing the old key with a new one) doesn't take all users offline.

⚠️ The wrong tool leaves data open

TL;DR: Encoding (Base64) only changes the format of data: it's two-way, has no key, and is not security, because anyone can reverse it with atob. Hashing (SHA-256, bcrypt) turns data into a fixed-length fingerprint: it's one-way, has no key, can't be reversed, and is used to check integrity (that data hasn't changed). Encryption (AES, RSA) is two-way and needs a key, to keep data secret when you need to read it again. Passwords use a slow hash (bcrypt/argon2), not encryption, because a leaked key opens all passwords at once. Base64 for a password is a big mistake. Certificate pinning is an extra technique: the app keeps the server's certificate inside its code, then refuses a certificate that doesn't match.

⚡ Encoding: a change of format, not a secret

Write:

const encoded = btoa("Hello");  // "SGVsbG8="
const decoded = atob("SGVsbG8="); // "Hello"

Meaning: btoa turns text into Base64, and atob turns it back. This is encoding: it only changes the format of data. Anyone can encode and decode, no key needed. So Base64 isn't security: a password in Base64 can be reversed with a single line of code.

⚡ Hashing: a one-way fingerprint

Meaning: Hashing turns any data into a string with a fixed length, and the result can't be turned back into the original data. A SHA-256 hash is always 64 hex characters (0 to 9 and a to f). For passwords, use a hash that's slow on purpose: bcrypt or argon2.

⚡ Encryption: two-way, needs a key

Write:

const cipher = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, message);
const back = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, cipher);

Meaning: AES-GCM is encryption: encrypt turns message into ciphertext (data that looks random) with the key key. Ciphertext can only be read again through decrypt with the right key. The iv is a random number used once, and it must be fresh for each encryption.

⚡ Passwords: hashing, not encryption

Guess first: why not just encrypt passwords, so they can be read again when needed?

Because encryption can be decrypted. If the key leaks, every password is open at once. A hash can't be reversed.

Meaning: Passwords use hashing. Secret data you need to read again uses encryption. File integrity (making sure a file hasn't changed) uses hashing. Binary data (such as an image) sent through text uses encoding.

What you will learn