Storing Passwords and Hashing

Use slow, salted password hashes like Argon2id or bcrypt, and know why fast hashes like MD5 and SHA are wrong for passwords.

Beginner · about 3 hours · difficulty 2/5

Learn first: Sessions and Cookies

Never store passwords, not even encrypted. Store a password hash: a one-way transformation that lets you verify a login attempt but not recover the original.

Fast hashes vs password hashes

  • MD5 and SHA-1 are broken for security and must not be used.
  • SHA-256 and SHA-3 are fine for integrity checks, signatures and checksums, but they are designed to be fast. An attacker with a GPU can try billions of guesses per second against a leaked SHA-256 hash.
  • Password hashing functions are deliberately slow and tunable: Argon2id (the current recommendation), scrypt and bcrypt. They include a random salt per password so identical passwords produce different hashes and precomputed rainbow tables are useless.

Practical rules

  • Use your language's well-known library (argon2, bcrypt) and its verify function, which compares in constant time.
  • Tune the cost so hashing takes roughly 100-500 ms on your hardware, and raise it over time; rehash on login when parameters change.
  • bcrypt truncates input at 72 bytes; be aware if you allow very long passphrases.
  • Rate-limit login attempts and consider checking passwords against known breach lists.

Related: HMAC and encryption

Use HMAC-SHA256 to sign data such as webhook payloads, and authenticated encryption (AES-GCM) via a vetted library when you must decrypt data later. Never invent your own crypto.

You are ready to move on when you can explain why SHA-256 is wrong for passwords but right for file checksums, and your app stores Argon2id or bcrypt hashes only.

Key concepts

  • Argon2id
  • bcrypt
  • scrypt
  • Why not MD5 or SHA for passwords
  • Salts and work factors
  • HMAC signatures
  • Symmetric encryption (AES-GCM)

Mini project

Benchmark SHA-256, bcrypt and Argon2id on your machine by hashing 1000 passwords, then implement signup and login using Argon2id with automatic rehashing when parameters change.

Check yourself

What does a salt protect against, and what does it not protect against?

Free resources