Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Vulnerable Components and Auth Failures

Password Storage and Password Policy

Store passwords safely with slow salted hashes like Argon2id or bcrypt, and write a modern password policy based on length, breached lists and no forced rotation.

When a database leaks, the damage depends on how passwords were stored. Plaintext passwords are instantly usable everywhere the user reused them. Fast, unsalted hashes can be reversed at billions of guesses per second. Properly stored passwords force an attacker to spend real time and money per guess. This lesson covers both halves of the problem: how to store passwords, and what policy to enforce at sign-up.

It follows Identification and Authentication Failures. Everything here is defensive; the demos only hash strings you supply.

Encryption is not hashing

  • Encryption is reversible with a key. If the key leaks, all passwords leak.
  • Hashing is one-way. You store a fingerprint and compare fingerprints at login. Nobody, including you, can read the password back.

Passwords should be hashed, never encrypted and never stored in plaintext.

What makes a password hash good

  1. Slow on purpose. A general hash like SHA-256 is designed to be fast. Fast is bad here, because attackers can test enormous numbers of guesses. Password hashes have a tunable cost.
  2. Salted. A salt is a random value stored next to the hash. It makes identical passwords produce different hashes and defeats precomputed tables.
  3. Memory-hard (ideal). Memory requirements make GPU and custom hardware attacks expensive.
  4. Upgradable. The stored string records the algorithm and cost so you can raise it later.

Recommended choices, in OWASP's usual order of preference: Argon2id, then scrypt, bcrypt (note its 72-byte input limit) or PBKDF2 with a high iteration count where compliance requires it. Use your language's maintained library; never write your own.

Storing a password

Random salt Unique for each user.
Slow hash Argon2id, scrypt, bcrypt.
Store the result Algorithm, cost, salt, hash.

Never store the password itself.

1 / 2

A runnable demonstration of salting and stretching

Pyodide on this site does not include a fast password-hashing function, so the stand-in below stretches SHA-256 with a loop. It shows the ideas (salt, cost, constant-time compare), but it is not a production algorithm. In real code, call Argon2id or bcrypt from a library.

Python Salt and stretch (teaching stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both users picked the same password, yet the stored values differ, so cracking one tells the attacker nothing about the other.

What real stored values look like

Libraries produce a self-describing string. These are illustrative, not real credentials:

TEXT
$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG
$2y$12$abcdefghijklmnopqrstuuK1mP0Qe8yJp3Jq7h5Z8o2Yw1xX9aN3a

The string records the algorithm, its cost parameters and the salt. At login you call the library's verify function and, if it reports that the cost is out of date, rehash and save the new value.

A modern password policy

Current guidance (NIST SP 800-63B and OWASP) favors usability that actually improves security:

  • Minimum length of at least 8, preferably 12 or more, and allow long passphrases (64 characters or more).
  • Allow all characters, including spaces and Unicode. Do not truncate silently.
  • Check against breached and common passwords. This beats composition rules.
  • Do not force periodic rotation without evidence of compromise; forced change produces Summer2025! then Summer2026!.
  • Do not require arbitrary composition ("one symbol, one digit") as the main rule.
  • Allow paste so password managers work.
  • Offer MFA (see Sessions, MFA and Lockout).
Python A small password policy checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that Summer2025! has upper case, a digit and a symbol but is still rejected: composition rules would accept it, a breach list does not. In production, check the breached list with a k-anonymity service so that the full password hash never leaves your server.

Common mistakes

  • Using a fast hash (MD5, SHA-1, plain SHA-256) for passwords.
  • One global salt (a "pepper" used alone), or no salt at all.
  • Writing your own algorithm or comparing hashes with == instead of a constant-time function.
  • Logging passwords or sending them in the URL.
  • Emailing passwords in plaintext, which means you store them reversibly.
  • Not planning migration: wrap old hashes with a stronger one and upgrade at the next login.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is SHA-256 alone a poor choice for storing passwords?

    Password hashes should be deliberately slow and ideally memory-hard. Fast general-purpose hashes make offline cracking cheap.
  2. 2. What does a salt achieve?

    A unique random salt per password forces the attacker to attack each hash separately.
  3. 3. Which password policy is closest to current guidance?

    Length, breached-password checks and passphrases improve security; forced rotation and strict composition rules mostly encourage predictable patterns.
  4. 4. How should you compare a stored hash with a computed one?

    Constant-time comparison avoids leaking, through timing, how many leading characters matched.

Summary

  • Hash passwords, never encrypt or store them in plaintext.
  • Use Argon2id (or scrypt, bcrypt, PBKDF2) through a maintained library, with a unique salt and tunable cost.
  • Prefer length, passphrases and breached-list checks to composition rules and forced rotation.
  • Compare in constant time and upgrade old hashes at login.

Next: Sessions, MFA and Lockout