Hashing vs Encryption: What Is the Difference?
Hashing and encryption both turn readable data into something that looks like noise, so they get lumped together. They solve different problems. Encryption hides data and can be undone by whoever holds the key. Hashing produces a fixed-size fingerprint and is designed so that it cannot be undone at all. Choosing the wrong one is how plaintext passwords and unverified downloads end up in production.
The core difference
Encryption is a two-way operation. You put in plaintext and a key, you get ciphertext, and with the right key you get the plaintext back. Its purpose is confidentiality: only key holders can read the data. A hash function is one-way. You put in data of any size and get a short fixed-length digest. There is no key and no decryption; the only way to "reverse" a hash is to guess inputs until one produces the same digest.
| Hashing | Encryption | |
|---|---|---|
| Reversible | No | Yes, with the key |
| Uses a key | No (HMAC adds one) | Yes |
| Output size | Fixed, whatever the input | About the size of the input |
| Typical goal | Integrity, fingerprints, password verification | Confidentiality |
| Examples | SHA-256, bcrypt, Argon2 | AES-GCM, ChaCha20-Poly1305, RSA |
A third thing often gets mixed in: encoding, such as Base64. It has no key and no secrecy at all, and it is fully reversible. We cover it in Base64 Is Encoding, Not Encryption.
What a hash looks like
A good hash is deterministic (same input, same digest), fast to compute, and has the avalanche property: changing one character changes the digest completely. Here are real digests of the five-character string hello, computed with Python's hashlib:
MD5 5d41402abc4b2a76b9719d911017c592
SHA-1 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA-256 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256 of "hellp" (one letter different):
fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7
The two SHA-256 values share nothing visible even though the inputs differ by one letter. The digest length is the same whether you hash one word or a 4 GB file. You can reproduce these with the Hash Generator, which shows SHA-1, SHA-256, SHA-384 and SHA-512 for the text you type, computed locally with your browser's Web Crypto API. It does not offer MD5, so the MD5 line above comes from Python (hashlib.md5(b"hello").hexdigest()). Remember it hashes the UTF-8 bytes of exactly what you type, so a trailing newline gives a different digest.
MD5 and SHA-1 are broken for security
A secure hash should make it infeasible to find two different inputs with the same digest (a collision). MD5 has been unsafe for that since collisions were demonstrated in 2004, and today they can be generated in seconds on ordinary hardware. SHA-1 lasted longer, but in 2017 researchers from CWI Amsterdam and Google published the first practical SHA-1 collision (the SHAttered attack): two different PDF files with the same SHA-1. Git used SHA-1 for object IDs and moved towards SHA-256 as a result, and certificate authorities stopped issuing SHA-1 certificates.
The SHA-2 family (SHA-256, SHA-384, SHA-512) has no known practical attacks and is the sensible default for fingerprints and signatures. Do not use MD5 or SHA-1 where an attacker could craft the input, such as signatures, certificates or deduplicating untrusted uploads. They remain fine as non-adversarial checksums, for example spotting accidental corruption, though SHA-256 costs little extra.
Checksums versus security
A checksum (CRC32, Adler-32) catches random bit flips in transit. It is not designed to resist someone who deliberately alters data, because they can compute a matching value. A cryptographic hash raises the bar, but publishing a hash next to a download only helps if the hash comes from a more trusted channel than the file itself. If an attacker can replace the file, they can replace the hash on the same page. Digital signatures, which bind the digest to a private key, are the real answer to "is this the file the author published?".
Passwords need slow, salted hashes
This is where the wrong choice does the most damage. Storing passwords in plaintext is obviously wrong, and encrypting them is nearly as bad: a stolen key decrypts everything, and you never need the original password anyway, only the ability to check a guess. So you store a hash. But plain SHA-256 is the wrong hash, because it is designed to be fast. A single modern GPU can compute billions of SHA-256 hashes per second, which means an attacker with a stolen database can try every common password and a huge amount of variations in minutes.
Two additions fix this:
- A salt: a unique random value per user, stored beside the hash and mixed into the input. Without salts, identical passwords share identical hashes, and attackers can use precomputed rainbow tables, huge lookup tables mapping digests back to common passwords. A salt makes every user's hash unique and makes precomputation worthless. The salt is not secret.
- A deliberately slow, tunable algorithm: purpose-built password hashes such as Argon2id, scrypt, bcrypt and PBKDF2 have a cost parameter that makes each guess take real time (and with scrypt and Argon2, real memory). Raise the cost as hardware improves. Current guidance (for example from OWASP) favours Argon2id where available, with scrypt, bcrypt or PBKDF2 as established alternatives.
Use your framework's password-hashing function, which generates the salt and encodes the parameters for you, and never write your own scheme. And remember that no hash rescues a weak password. A random, long password from a tool like the Password Generator is far harder to crack than a human-chosen one.
HMAC: a hash with a key
A plain hash cannot prove who made a message, since anyone can compute it. HMAC (hash-based message authentication code) mixes a secret key into the hash, so only holders of the key can produce or verify the tag. It is the standard way to authenticate webhooks and API requests, and it is what the HS256 algorithm in JSON Web Tokens uses (HMAC with SHA-256), as explained in How JWT Works. You can experiment with HS256 tokens in the JWT Generator. Do not build this as SHA256(key + message); use a real HMAC implementation and compare tags with a constant-time function.
When you actually need encryption
If you must read the data back, such as a stored API token, a file or a message, use authenticated encryption: AES-256-GCM or ChaCha20-Poly1305. These are symmetric ciphers (the same key encrypts and decrypts) and "authenticated" means tampering is detected. Never reuse a nonce with the same key, never roll your own mode, and store keys separately from data in a key-management service. Public-key systems such as RSA and elliptic-curve schemes solve key exchange, where two parties who have never met need to agree on a secret.
A quick decision guide:
- Need to verify a password? Slow salted hash (Argon2id, bcrypt, scrypt, PBKDF2).
- Need to detect changes in data, or fingerprint it? SHA-256 or better.
- Need to prove a message came from a key holder? HMAC, or a digital signature.
- Need to keep data secret but recoverable? Authenticated encryption such as AES-GCM.
- Just need bytes to fit in a text field? Encoding, with the Base64 Encoder / Decoder, and no security expectations.