Skip to content

Encryption vs Hashing vs Encoding: What Each One Is For

Encryption vs Hashing vs Encoding: What Each One Is For

People mix these three up all the time, and the mix-up has caused real damage. Encoding changes how data is written so it can travel safely, and anyone can reverse it. Hashing produces a fixed fingerprint of the input that cannot be turned back into the original. Encryption scrambles data so that only someone holding the right key can read it. For passwords, the answer is hashing with a deliberately slow algorithm, not encryption and not encoding.

Start with one question: can it be reversed, and does it need a secret?

Most confusion goes away once you ask two things. Can the original be recovered from the output? And does recovery require a secret key? Encoding is reversible with no key at all. Encryption is reversible, but only with a key. Hashing is not reversible by design.

Those answers decide what each one is good for. A Base64 string in a URL protects nothing. An encrypted file protects its contents only as long as the key stays secret. A hash protects a value by making the original unrecoverable from the output, though that protection is weaker than most people assume once the input is guessable.

Encoding, hashing and encryption compared Three horizontal flows. Top: text bytes pass through a Base64 mapping to a Base64 string, with no key. Middle: any input passes through a SHA-256 digest function to a fixed 256-bit digest, which cannot be reversed. Bottom: plaintext passes through an AES cipher with a secret key to produce ciphertext. Encoding (Base64): no key, reversible by anyone Text bytes Base64 mapping Base64 string Hashing (SHA-256): no key, one way Any input Digest function Fixed 256-bit digest Encryption (AES): secret key, reversible Plaintext Cipher with key Ciphertext
Conceptual diagram by UnpanicTech. It shows the data flow of each operation, not a product or a capture from a real system.

Encoding is a format change, not a secret

Encoding rewrites data into another representation so it survives transport. Base64, defined in RFC 4648 [3], turns binary bytes into a set of printable characters. Email attachments and some API payloads use it for that reason. URL encoding does something similar for characters that have special meaning in a URL.

The important part is that there is no key. Anyone who sees a Base64 string can decode it in one line. That makes encoding useful for compatibility and useless for confidentiality. If you ever see a token or a "protected" value that is only Base64, treat it as plain text.

Hashing gives you a fingerprint you cannot turn back

A cryptographic hash function takes an input of any length and produces a fixed-length output. SHA-256, for example, always produces 256 bits. The same input always gives the same output, and a small change to the input changes the output completely. Hashing is one-way in practice: you cannot compute the input from the digest except by guessing candidate inputs and comparing results.

That makes hashes good for checking integrity, such as confirming that a downloaded file matches a published checksum. It also makes them useful for storing values you only need to compare, not read back. Keep in mind that a plain hash is not secret. Anyone can hash a guess and compare. If you need a hash that only a key holder can produce, that is a keyed construction such as HMAC, which is a different tool.

Encryption is reversible on purpose

Encryption is the opposite. Its whole job is to let an authorized party recover the original. Symmetric encryption uses one secret key for both directions. AES, standardized in FIPS 197 [5], is the common choice. Asymmetric encryption uses a public and private key pair, which solves the problem of sharing a secret key in advance.

Use encryption when the data must be read again later: backups, files at rest, messages in transit. Pick an authenticated mode, so that tampering is detected and not just hidden. The hard part is rarely the cipher. It is key management: where the key lives, who can read it, and how it rotates. An encrypted database with its key sitting next to it protects very little.

Why encrypting passwords is the wrong move

This is the mistake that matters most. If you encrypt passwords, you have to store the key somewhere the application can reach. Whoever steals the database and the key gets every password in plain text. Even without a breach, your own staff and support tools could decrypt them, and there is no good reason for anyone to ever need the original password. A login system only needs to check whether a guess matches.

Encoding is worse. Base64 is not protection in any sense. Plain fast hashes such as raw SHA-256 are also a bad fit. They are designed to be quick, and quick is exactly what an attacker wants when testing billions of guesses offline on modern hardware. A password store needs a hash that is deliberately slow and memory-hungry, and that accepts a per-user salt.

How to store passwords properly

NIST's digital identity guidelines [1] and the OWASP Password Storage Cheat Sheet [2] point to the same design. Use a password-hashing function such as Argon2id, which is specified in RFC 9106 [4]. If Argon2id is not available in your stack, scrypt or bcrypt are acceptable alternatives, and PBKDF2 with a strong iteration count is the approved fallback in some regulated environments. Each user gets a random salt, so identical passwords produce different stored values. The salt is not a secret, and it is stored next to the hash.

Salted password storage flow The user's password and a random per-user salt enter a memory-hard key derivation function such as Argon2id. The function produces a hash. The database stores the salt and the hash together. The original password is never stored. Password (typed) Random salt (per user) Argon2id Slow and memory-hard Stored record salt plus hash
Conceptual diagram by UnpanicTech. The password itself is never written to storage.

Here is a minimal example using Python's standard library and scrypt. It is a starting point, not a complete authentication system. Tune the cost parameters on your own hardware, and prefer Argon2id where your libraries support it.

import hashlib
import hmac
import secrets

SCRYPT_PARAMS = dict(n=2**17, r=8, p=1, dklen=32, maxmem=2**28)

def hash_password(password: str) -> str:
    salt = secrets.token_bytes(16)
    digest = hashlib.scrypt(password.encode("utf-8"), salt=salt, **SCRYPT_PARAMS)
    return salt.hex() + "$" + digest.hex()

def verify_password(password: str, stored: str) -> bool:
    salt_hex, digest_hex = stored.split("$")
    candidate = hashlib.scrypt(password.encode("utf-8"), salt=bytes.fromhex(salt_hex), **SCRYPT_PARAMS)
    return hmac.compare_digest(candidate, bytes.fromhex(digest_hex))

A few things in that snippet matter more than they look. The salt comes from a cryptographically secure random source, not from a timestamp or a username. The comparison uses a constant-time function, so the response time does not leak how many leading bytes matched. And the cost parameters are what make offline guessing expensive, so they should be raised as hardware improves.

Two practical notes. Some older systems cap the input length of bcrypt at 72 bytes, so check your library's behavior before relying on it for long passphrases. And a pepper, a secret added to every hash and stored outside the database, can add a layer of defense, but it only helps if you manage it carefully. It is not a replacement for a slow, salted hash.

Where the three meet in a real system

Most production systems use all three, each for a different job. A web service might encode a binary token for transport, hash a password for storage, and encrypt session data or backups with keys held in a dedicated key store. Mixing them up is what causes trouble. Someone encodes a secret and thinks it is hidden. Someone hashes a value that later needs to be read back. Someone encrypts passwords and then guards the key as if it were the only risk.

If you are reviewing an existing system, start by listing every place a sensitive value is stored or transformed, then ask the reversibility question for each one. Any value that must be recovered needs encryption with real key management. Any value that only needs checking needs a hash with the right properties. Anything that is merely encoded should be assumed readable by anyone who sees it.

Passwordless and phishing-resistant sign-in reduces how much password storage you need in the first place. Our guide on phishing-resistant MFA with FIDO2 and conditional access covers that side of the problem.

What this does not settle

This article covers the core distinctions and a safe default for passwords. It does not cover post-quantum migration, key ceremony design, hardware security modules, or the many ways a password can be stolen before it ever reaches a hash function, such as phishing or malware on an endpoint. Parameter recommendations also change over time, so treat the numbers in the example as a starting point and check current guidance before deploying.

Sources & References

  1. NIST SP 800-63B, Digital Identity Guidelines: Authentication and Lifecycle Management
  2. OWASP Password Storage Cheat Sheet
  3. RFC 4648, The Base16, Base32, and Base64 Data Encodings
  4. RFC 9106, Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications
  5. FIPS 197, Advanced Encryption Standard (AES)
Related Cryptography & Credential Hygiene Guides

Even strong Argon2id password hashes won't save an environment if developers commit cleartext API keys to Git or rely on weak TLS certificates. Explore our security hardening guide for full-stack cryptographic hygiene:

NK

Cybersecurity Researcher & Technical Editor

UnpanicTech publishes independent cybersecurity research and defensive security analysis. Naseem Khan serves as Technical Editor, with a focus on vulnerability analysis, defensive security, incident response, cloud security, and practical security engineering.

Leave a Comment (Authenticated Users)