Hashing & Password Storage: Salts, bcrypt, Argon2 & What Not to Do

A password must never be stored β€” only the result of a slow, salted, one-way transformation of it. Learn how hashing works, why fast hashes fail for passwords, and what bcrypt and Argon2 do differently.

πŸ“‚ Developer Essentials ⏱ About 12 minutes πŸ“Ά Intermediate πŸ› οΈ Pairs with: Hash Generator

What you will learn

  • Explain what a hash function guarantees β€” and what it doesn't
  • Tell hashing apart from encryption
  • Explain salts, peppers and work factors in plain language
  • Choose between bcrypt, scrypt and Argon2
  • Spot the password-storage mistakes behind real breaches
In this guide
  1. What a hash function is
  2. Hashing is not encryption
  3. How password storage should work
  4. Why breaches still happen
  5. Common mistakes
  6. Try it: Hash Generator
  7. Frequently asked questions

What a hash function is

A hash function takes input of any size and returns a fixed-size fingerprint β€” a digest. SHA-256 always returns 256 bits (64 hex characters), whether the input is one letter or a whole film. Good hash functions have four properties:

  1. Deterministic

    The same input always gives the same output. No randomness, no exceptions.

  2. One-way

    You cannot reconstruct the input from the output. The only way to β€œreverse” a hash is to guess inputs until one matches.

  3. Avalanche effect

    Changing one bit of the input changes about half the output bits. password and passwore have nothing visibly in common as hashes.

  4. Collision-resistant

    It is infeasible to find two different inputs with the same output. (Not impossible in theory β€” just more work than anyone can do.)

Try it: hash hello and hellp in the Hash Generator and compare β€” every character of the digest changes. That is the avalanche effect, and it is what makes hashes useful as fingerprints: if a downloaded file's SHA-256 matches the publisher's, the file was not tampered with.

Hashing is not encryption

This is the confusion that causes the most damage. Encryption is reversible β€” ciphertext plus the key gives back the plaintext, by design. Hashing is one-way β€” there is no key and no decryption. They solve different problems: encryption protects data you need to read again; hashing verifies data you never need to read again.

That is exactly the password case. A login system never needs to know your password β€” it only needs to check whether the password you typed matches the one you chose at signup. So it stores a hash, and at login it hashes what you typed and compares digests. If the database leaks, the attacker gets hashes, not passwords β€” provided the hashing was done right, which is the subject of the rest of this guide.

How password storage should work

Storing SHA-256(password) is not β€œdone right”. A fast hash is a gift to an attacker with a GPU: modern hardware tries billions of SHA-256 guesses per second, and common passwords fall in minutes. Proper password storage has three ingredients:

  1. A unique salt per password

    A salt is a random string (16+ bytes) generated for each user and stored next to the hash. It means identical passwords get different hashes β€” so an attacker cannot precompute one rainbow table and crack every account at once, and cannot tell which users share a password.

  2. A slow hash with a work factor

    Password hashes must be deliberately slow β€” hundreds of milliseconds per guess instead of nanoseconds. The work factor (cost) tunes that slowness, and it should rise as hardware gets faster.

  3. A memory-hard design (modern best practice)

    GPUs crack fast hashes in parallel. Memory-hard functions force each guess to use a chunk of RAM, which GPUs have little of per core β€” levelling the field.

AlgorithmTypeUse it?
bcrypt (1999)Adaptive, salt built inYes β€” the safe default, battle-tested
scryptMemory-hardYes β€” good, needs careful parameters
Argon2idMemory-hard, OWASP's pickYes β€” the modern recommendation
PBKDF2Iterated HMACAcceptable with high iterations (600k+ for SHA-256)
MD5 / SHA-1 / plain SHA-256Fast general-purpose hashesNo β€” never for passwords

Verification then works like this: take the password the user typed, take the stored salt, run the same slow function, and compare the result with the stored hash using a constant-time comparison (so the time taken doesn't leak how many characters matched). The plaintext password is never written to disk.

Why breaches still happen

Almost every β€œmillions of passwords leaked” headline traces back to one of a short list of failures: passwords stored in plaintext; passwords hashed with MD5 or SHA-1 and no salt; one salt shared by all users; or a home-grown algorithm the author believed was clever. In each case the attacker didn't break the mathematics β€” they walked through a door the developers left open.

The defence in depth around hashing matters too: require strong passwords (or better, passkeys), rate-limit login attempts, alert on unusual logins, and assume the database will leak one day β€” because the question is not whether your storage is perfect, but how expensive you make the leak.

A pepper is not a salt. A pepper is a secret key stored separately from the database (in the application config or a vault) and mixed into every hash. It adds real protection β€” a database leak alone is not enough β€” but it is an addition to salting, never a replacement for it.

Common mistakes

  • Storing passwords in plaintext or β€œencrypted”. Encryption is reversible β€” anyone with the key (or the database and the code) reads every password.
  • MD5/SHA-1/SHA-256 with no salt. Billions of guesses per second, rainbow tables, identical passwords visible at a glance.
  • One salt for everyone. A shared salt is barely better than no salt β€” precompute once, crack everyone.
  • Inventing your own algorithm. Cryptography is where β€œI wrote my own” reliably means β€œI made a mistake I can't see”. Use bcrypt, scrypt or Argon2.
  • Truncating passwords. Cutting a password to 8 or 16 characters before hashing throws away the entropy the user supplied.
  • Logging passwords. Passwords in application logs end up in log aggregators, backups and support tickets. Never log authentication input.

Try it: Hash Generator

Type a word into the Hash Generator and see its SHA-256 change completely when you change one letter β€” the avalanche effect β€” then read on to learn why SHA-256 alone is still the wrong tool for passwords.

Open the Hash Generator β†’

Keep learning

Frequently asked questions

Can a hash be reversed?

Not directly β€” there is no decryption. Attackers guess: they hash likely passwords (dictionaries, leaked password lists, brute force) until one matches. Slow, salted hashes make each guess expensive, which is the whole defence.

Is SHA-256 secure?

As a general-purpose hash, yes β€” for file integrity, certificates and HMAC it is the standard. For passwords, no: it is too fast. Security depends on the job, and password storage needs a slow, salted function like Argon2id or bcrypt.

What exactly is a salt?

A random value, unique per password, stored alongside the hash and mixed into the hashing. It stops precomputed rainbow tables and makes identical passwords hash differently. It is not secret β€” it is stored in plaintext next to the hash β€” and that is fine, because its job is uniqueness, not secrecy.

If the database leaks, are salted bcrypt passwords safe?

Much safer than plaintext or fast hashes β€” cracking each one costs real time and hardware. But weak passwords (123456, qwerty) still fall quickly to dictionary attacks, which is why password strength and breach monitoring still matter even with perfect storage.

Should I encrypt passwords instead of hashing them?

No. Encryption is reversible, so anyone who gets the key reads every password. Hashing is one-way by design β€” the system never needs the original password, only a way to check a guess against it.