Skip to content
Velloreth
Cybersecurity 4 min read

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

The two primitives

HashEncryption
Directionone-wayreversible
Keynonerequired
Outputa fixed sizethe same size as the input
Useintegrity, a passwordconfidentiality

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.

AlgorithmNotes
Argon2idthe modern default, memory-hard
bcryptwidely supported, well tested
scryptmemory-hard
PBKDF2older, still acceptable
SHA-256too fast, do not use
MD5broken, 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

AlgorithmStatus
MD5broken, a collision is trivial
SHA-1broken, a collision is practical
SHA-256secure
SHA-3secure
bcryptsecure for a password
Argon2idsecure 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.

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.

Frequently asked questions

What is the difference between hashing and encryption?
A hash is a one-way function: the input produces a fixed-size output and the input cannot be recovered. Encryption is reversible with a key. A hash is used to verify integrity or to store a password, and encryption is used to hide data.
Why is a password hashed and not encrypted?
Because the server must not be able to recover the password. If it is encrypted, the server holds the key and a compromise reveals every password. A hash is one-way, so a compromise reveals only the hashes, and a slow algorithm makes a brute force expensive.
What is a salt?
A random value added to the input before hashing, stored with the hash. It ensures that two users with the same password have different hashes, which defeats a precomputed table and a rainbow table. The salt does not need to be secret.
What is a pepper?
A secret value added to the input, stored separately from the hash, such as in the application configuration. It adds a layer that a database compromise alone does not defeat. It is optional and it is not a substitute for a salt.
Which hash should I use for a password?
Argon2id, bcrypt or scrypt, which are deliberately slow and memory-hard. Never use SHA-256 or MD5 for a password, because they are fast and a GPU can compute billions per second. Tune the cost so a single hash takes a noticeable fraction of a second.
Which hash should I use for integrity?
SHA-256 or SHA-3. MD5 and SHA-1 are broken for a security use, because a collision can be constructed. For a file download, publish the SHA-256 and compare it.