Hashing vs Encryption: What the Difference Is
A hash is one-way and an encryption is reversible. Learn the uses, the algorithms, and why a password is hashed not encrypted.
By Velloreth
The two primitives
| Hash | Encryption | |
|---|---|---|
| Direction | one-way | reversible |
| Key | none | required |
| Output | a fixed size | the same size as the input |
| Use | integrity, a password | confidentiality |
A hash verifies, and an encryption hides.
The hash
A one-way function.
digest = SHA-256("hello")
The properties:
- A fixed-size output, whatever the input size
- The same input always gives the same output
- A small change gives a completely different output
- The input cannot be recovered
- A collision is hard to find
The uses:
- Verify a download
- Store a password
- Build a cache key
- Detect a duplicate
- A content address
The encryption
Reversible with a key.
ciphertext = encrypt(plaintext, key)
plaintext = decrypt(ciphertext, key)
The uses:
- A file at rest
- A message in transit
- A field in a database
- A backup
The key is the secret, and the algorithm is public.
The password
A password is hashed, never encrypted.
Why: the server must not be able to recover it. If it is encrypted, the server holds the key, and a compromise reveals every password.
The algorithm must be slow.
| Algorithm | Notes |
|---|---|
| Argon2id | the modern default, memory-hard |
| bcrypt | widely supported, well tested |
| scrypt | memory-hard |
| PBKDF2 | older, still acceptable |
| SHA-256 | too fast, do not use |
| MD5 | broken, do not use |
A fast hash is a GPU’s friend. A GPU computes billions of SHA-256 per second and a few thousand bcrypt.
The cost is tuned so a single hash takes a noticeable fraction of a second.
The salt
A random value added to the input before hashing.
hash = bcrypt(password + salt)
It is stored with the hash, and it does not need to be secret.
Why it matters: two users with the same password have different hashes, which defeats a precomputed table.
A rainbow table is a precomputed table of hashes, and a salt makes it useless.
A unique salt per user is the rule.
The pepper
A secret value added to the input, stored separately.
hash = bcrypt(password + pepper)
The pepper is in the application configuration, not in the database.
It adds a layer that a database compromise alone does not defeat.
It is optional, and it is not a substitute for a salt.
The integrity
A hash verifies a download.
sha256sum file.tar.gz
# compare with the published value
A signature is stronger, because it also proves the origin.
A checksum is a hash, and it detects an accidental corruption.
A MAC is a keyed hash, and it detects a deliberate modification.
The broken algorithms
| Algorithm | Status |
|---|---|
| MD5 | broken, a collision is trivial |
| SHA-1 | broken, a collision is practical |
| SHA-256 | secure |
| SHA-3 | secure |
| bcrypt | secure for a password |
| Argon2id | secure for a password |
A collision is two inputs with the same hash, which breaks the integrity guarantee.
MD5 and SHA-1 are still fine for a non-security use, such as a cache key, and they must not be used for a signature or a password.
Common mistakes
A password encrypted. The server can recover it.
A fast hash for a password. A GPU cracks it.
No salt. A rainbow table.
A shared salt. A precomputed table per salt.
MD5 or SHA-1 for a security use. A collision.
A hash used for confidentiality. The input is not hidden.
A comparison with ==. A timing attack.
A pepper in the database. It is not a pepper.
A cost that is too low. A fast brute force.
A cost that is too high. A slow login.
A quotable summary
A hash is one-way and an encryption is reversible with a key, so a hash verifies integrity and an encryption hides data. A password is hashed with a slow algorithm such as Argon2id or bcrypt, never encrypted, and a unique salt per user defeats a precomputed table. MD5 and SHA-1 are broken for a security use, and a fast hash is a GPU’s friend.
Related reading
For the related topics, see password-storage-best-practices and encryption-basics. For the broader context, encryption-at-rest-vs-transit and zero-trust-for-small-teams.