Three words get mixed up in code reviews and security questionnaires: encoding, encryption and hashing. They look similar on screen, because all three turn readable text into a string of letters and digits, but they promise very different things. This article tells them apart with real values, then explains the mistake that causes the most damage: storing passwords with a plain hash.
Encoding, encryption, hashing
| Needs a key? | Can be undone? | Typical use | |
|---|---|---|---|
| Encoding (Base64, URL, hex) | no | yes, by anyone | carry bytes through text channels |
| Encryption (AES, RSA) | yes | yes, with the key | keep data secret |
| Hashing (SHA-256, bcrypt) | no (HMAC: yes) | no | fingerprints, integrity, password check |
If a value must come back, it is encoding or encryption. If you only need to compare, to ask “is this the same as before”, it can be a hash. The article Base64 is not encryption covers the first row; this one is about the third.
What a hash looks like
Put the word hello into the hash generator and you get one fixed-size fingerprint per algorithm:
MD5 5d41402abc4b2a76b9719d911017c592
SHA-1 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA-256 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
The lengths are fixed: 128 bits (32 hex digits) for MD5, 160 bits (40) for SHA-1, 256 bits (64) for SHA-256, and 384 and 512 bits for SHA-384 and SHA-512. A whole book hashes to the same length as one word.
Add one character, hello!, and the whole digest changes:
MD5 5a8dd3ad0756a93ded72b823b19dd877
SHA-1 8f7d88e901a5ad3a05d8cc0de93313fd76028f8c
SHA-256 ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b
This is the avalanche effect: a small change in the input flips about half of the output bits, so a digest tells you nothing about how close two inputs are. It is also deterministic: the same input always gives the same output, on any machine. That is what makes a hash useful for checking that a download is intact or that two files are identical.
A hash is not a lock. Output and input have no key between them. What protects the input is only that nobody can work backwards from the digest, and that is where passwords go wrong.
Why a plain hash is a bad way to store passwords
Suppose a database stores SHA-256(password). After a leak the attacker does not need to reverse anything. They take a list of likely passwords, hash each one and look for a match. Two things make this easy:
- Hashes are fast. SHA-256 was built to process data quickly. One graphics card can compute billions of them per second, so an eight-letter password falls in hours or less.
- Hashes are shared. The digest of
hellois the same everywhere, so precomputed tables of digests for millions of common passwords turn a leaked column into a list of passwords, and everyone who chose the same password is found at once.
The fix has two parts. A salt, a random value stored with each password and mixed in before hashing, makes every digest unique, so a table is useless and two users with the same password get different values. And the function itself must be slow on purpose, so that every guess costs the attacker real time.
bcrypt: salt and cost built in
bcrypt does both. Hash a password in the bcrypt generator and you get one string that holds everything needed to check it later:
$2b$04$zbU96AfoMeWl7HPW9ebSAOnSxYHrqqDlM8OovK8vSAnLukTvysdty
It splits into $2b$ (the version), 04 (the cost), 22 characters of salt and 31 characters of digest, 60 characters in total. Checking is done by the same tool in Verify mode: it reads the salt and cost from the hash, hashes the password you give with them, and compares. The hash above matches correct horse battery staple and nothing else.
Two things to notice:
- The same password gives a different hash every time, because the salt is random. Both hashes still verify. You cannot compare two bcrypt strings with
==; you always call verify. - The cost is a power of two. Cost 4 means 2^4 = 16 rounds, cost 10 means 1,024. Each step up doubles the time of one hash, for you and for an attacker. Choose the highest cost your login can bear (OWASP suggests at least 10; the tool warns about anything lower), and raise it as hardware improves.
Mind the limit: bcrypt uses only the first 72 bytes of a password. The tool warns when a password is longer, because the rest is silently ignored. scrypt and Argon2 have no such limit and also make attackers spend memory, which is why new systems often choose Argon2id; bcrypt remains a sound, widely supported choice.
HMAC: a hash with a key
A hash on its own proves nothing about who made it. If you publish a file and its SHA-256, an attacker who changes the file can publish a new SHA-256 too. HMAC mixes a secret key into the hash, so only someone who holds the key can produce the right value. With the key key, the HMAC-SHA256 of The quick brown fox jumps over the lazy dog is:
f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
The hash generator has an HMAC key box that computes this. HMAC is what signs a JWT with HS256 (read how in JWT decoding is not verifying) and what most webhook providers use so that you can check a request really came from them. Compare the received and the computed values in constant time, not with a normal string comparison.
Broken hashes
MD5 and SHA-1 are no longer safe where an attacker can influence the input. Collisions, two different inputs with the same digest, were found for MD5 in 2004 and shown for SHA-1 in 2017. That rules them out for signatures and certificates. They are still fine as a quick checksum against accidental damage, but when in doubt use SHA-256.
Which tool for which job
| Task | Use |
|---|---|
| Check that a file arrived intact | SHA-256 |
| Detect duplicates, make a cache key | SHA-256 (or any fast hash) |
| Prove a message came from someone with a key | HMAC-SHA256 |
| Store a password | bcrypt, scrypt or Argon2id |
| Keep data secret and get it back | encryption (AES-GCM), with a managed key |
| Carry bytes in JSON or a URL | encoding (Base64) |
Everything you hash or check in these tools is processed in your browser and never uploaded. The text is not kept as a draft on your device, and an HMAC key is never saved or put in a share link.
Frequently asked questions
What is the difference between hashing, encryption and encoding?
Encoding (Base64, URL encoding) changes the form of data and needs no key, so anyone can undo it. Encryption hides data with a key and can be undone by whoever has the key. Hashing turns data of any size into a fixed-size fingerprint and cannot be undone: there is no key and no 'unhash'. They solve different problems and are not interchangeable.
Can a hash be reversed?
Not by calculation, but short or common inputs can be found by guessing: the attacker hashes candidates until one matches. MD5 of 'hello' is in every lookup table on the internet. That is why the speed of the hash decides how safe a stored password is, and why a salt is needed.
Is SHA-256 good enough for passwords?
No. SHA-256 is designed to be fast, and a single graphics card can try billions of guesses per second. A password hash must be slow and tunable: bcrypt, scrypt or Argon2. SHA-256 is right for file integrity, signatures and HMAC, where speed is a good thing.
Why are MD5 and SHA-1 considered broken?
Researchers can build two different inputs with the same MD5 (since 2004) or SHA-1 (shown in public in 2017). That breaks them for signatures and certificates. They still work as plain checksums against accidental corruption, but use SHA-256 for anything an attacker could influence.
What does the HMAC key add?
A plain hash proves nothing about who made it: anyone can hash a modified file and publish the new digest. HMAC mixes in a secret key, so only someone who has the key can produce the right value. It is the basis of the HS256 signature of a JWT and of most API request signing.
More guides
- CIDR notation: why a /31 has two usable hosts, what a /32 is for, and the subnet mistakes to avoid
- Unix timestamps: seconds, milliseconds or microseconds, and the dates where they go wrong
- UUID v4, UUID v7 and ULID: which ID to use, and what is inside each
- Base64 is not encryption: what it does, and the bugs it causes
- JSON, CBOR, MessagePack and Protobuf: the same data in four sets of bytes
- Regex backtracking: how one innocent pattern can freeze a server (ReDoS)
- URL encoding pitfalls: %20 or +, double encoding, and the characters that break links
- Cron and time zones: why your 02:30 job runs twice or never
- CSV edge cases: quotes, newlines, delimiters and the leading zero
- Why JSON silently changes your big numbers (and how to stop it)
- JWT pitfalls: decoding a token is not verifying it
- YAML surprises: the Norway problem, octal numbers and version strings