Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 40 ยท Authentication and Keys

Password Hashing: PBKDF2, scrypt and Argon2

Why passwords need slow salted hashes, with working Python for salts, rainbow tables, PBKDF2 and scrypt, work factors, peppers and Argon2 guidance.

Every site that lets you sign in has to store something to check your password against. The worst answer is the password itself: one database leak and everyone's password is public, including the ones people reuse on other sites. The next-worst answer is a plain fast hash such as SHA-256(password). That seems safe, because a hash cannot be reversed, but passwords are not random. People choose short, predictable passwords, so an attacker simply hashes millions of likely guesses and compares.

A password hash is a function designed for exactly this problem. It is deliberately slow and salted, so that guessing is expensive for the attacker while remaining affordable (one hash per login) for the server. This lesson explains salts, rainbow tables, work factors and the three standard algorithms: PBKDF2, scrypt and Argon2.

Why a fast hash is the wrong tool

A cryptographic hash like SHA-256 is built to be fast: a modern graphics card computes billions of them per second. Speed is a virtue for checksums and a disaster for passwords. If an eight-character lowercase password has 268, about 2 x 1011, possibilities, then a few billion hashes per second searches the whole space in minutes. Real passwords come from a far smaller set of "human" choices, so the attack is faster still.

Password storage therefore needs two things a plain hash lacks:

  1. A salt: a random value, unique per user, stored next to the hash.
  2. A work factor: a tunable cost that makes each guess slow (and, for scrypt and Argon2, memory-hungry).

Salts defeat precomputation

Without a salt, the same password always gives the same hash. An attacker can compute the hashes of a huge list of candidate passwords once and keep them in a lookup table (a rainbow table is a space-saving version of the same idea). Every stolen database can then be checked with plain lookups, and users who chose the same password are visible as identical hashes.

A salt breaks both problems. The stored value is hash(salt + password), and the salt is a fresh random 16 bytes for each account. A precomputed table is useless, because the attacker would need a separate table for every possible salt. Two users with the same password get completely different hashes. The salt is not secret; its only job is to be unique.

Python Unsalted hashes fall to a lookup table

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.

Without salts the entire database falls to a single table, and the equal hashes of alice and dave expose that they share a password. With salts, the table finds nothing, and the attacker has to hash guesses separately per account. The carol password (random and long) survives in both cases, because it is not in any list. That is the second lesson: no hashing scheme rescues a very weak password, it only raises the cost of guessing.

Slowness: the work factor

A salt stops precomputation but not the straightforward guessing attack. To slow that, use a function that costs a fixed, tunable amount of work. The classic way is to iterate: apply the hash many times in a way that cannot be shortcut. The standard is PBKDF2 (Password-Based Key Derivation Function 2, RFC 8018): it computes HMAC thousands of times, feeding the output of each round into the next, and XORs the intermediate results.

flowchart LR
    P["Password"] --> F["PBKDF2<br/>HMAC repeated<br/>N times"]
    S["Random salt"] --> F
    N["Iteration count N"] --> F
    F --> H["Derived hash<br/>stored with salt and N"]

The server stores the salt, the iteration count and the result, never the password.

Python provides it as hashlib.pbkdf2_hmac(hash_name, password, salt, iterations). The in-browser Python runtime on this site does not include that function, so the examples below use a small pure-Python stand-in built on hmac. It follows RFC 8018 exactly and is checked against published test values. On a normal Python installation, call hashlib.pbkdf2_hmac instead: it is written in C and far faster.

Python PBKDF2 stand-in with storage and verification

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.

The stored string carries everything verification needs: the algorithm, the iteration count (so you can raise it later without breaking old records), the salt and the digest. This self-describing format is why you will see strings such as $2b$12$... or $argon2id$v=19$m=... in real databases.

How much work is enough?

The cost to the attacker scales linearly with the iteration count. The next program counts the HMAC operations an attacker needs to try a list of guesses at different settings. Counting operations is more informative than timing, because it does not depend on how fast the machine is.

Python What the iteration count costs an attacker

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.

At one iteration the whole billion-word list takes a tenth of a second; at 600,000 iterations (a figure commonly recommended for PBKDF2-HMAC-SHA256 in recent guidance) it takes about 17 hours, for a single user. The server pays 600,000 iterations once per login, which is a fraction of a second. That asymmetry is the whole design. Check current recommendations from OWASP or NIST when you choose a value, since they rise as hardware improves.

Memory-hard functions: scrypt and Argon2

PBKDF2 only costs CPU time, and attackers have specialised hardware (GPUs, FPGAs, ASICs) that runs thousands of copies in parallel cheaply. A memory-hard function also needs a large amount of RAM per guess, and memory is expensive to replicate on many cores. This narrows the advantage of specialised hardware.

scrypt (RFC 7914) fills a big table of pseudo-random blocks and then reads from it in an unpredictable order, so a guess cannot be computed without keeping the whole table around. Its parameters are N (the cost, a power of two), r (block size) and p (parallelism); memory use is about 128 * N * r bytes. The pure-Python implementation below is slow and for learning only; it checks out against the test vector in the RFC.

Python scrypt from scratch and its memory cost

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 what the memory column means for an attacker. At the commonly recommended minimum of N = 217 and r = 8 (128 MiB), a card with 24 GiB of memory can run only a couple of hundred guesses at once, while the same card could run many thousands of PBKDF2 computations in parallel. Python's real hashlib.scrypt(password, salt=..., n=..., r=..., p=..., maxmem=...) is fast and available in standard builds, but not in the browser runner used here.

Argon2 won the Password Hashing Competition in 2015 and is the recommended choice for new systems. It comes in three variants; Argon2id (a hybrid that resists both side-channel and GPU attacks) is the one to use. Its parameters are memory cost m, time cost t (passes over memory) and parallelism p. Argon2 is not in Python's standard library, so with the argon2-cffi package it looks like this (read-only; it does not run in your browser):

Python
from argon2 import PasswordHasher

ph = PasswordHasher(time_cost=3, memory_cost=64 * 1024, parallelism=4)   # 64 MiB
stored = ph.hash("correct horse battery staple")
# '$argon2id$v=19$m=65536,t=3,p=4$<salt>$<hash>'   (salt is random and embedded)

ph.verify(stored, "correct horse battery staple")      # returns True, or raises VerifyMismatchError
print(ph.check_needs_rehash(stored))                   # True later if you raise the parameters

Pepper

A pepper is a secret value, the same for all users, that is mixed into the hash and kept outside the database (in an environment variable, a secrets manager or a hardware module). If only the database leaks, the attacker is missing the pepper and cannot even start guessing. One simple and safe way is to first compute HMAC(pepper, password) and then feed that result into the password hash. A pepper is an extra layer, not a replacement for salts and slow hashing, and it needs a plan for rotation because changing it invalidates existing records.

Practical checklist

  • Use Argon2id if available; otherwise scrypt, or PBKDF2-HMAC-SHA256 with a high iteration count where regulation requires it (FIPS).
  • Generate a fresh random salt per password (at least 16 bytes from os.urandom or secrets).
  • Store the algorithm, parameters and salt with each hash, so you can upgrade later and re-hash a password when the user next logs in.
  • Compare with hmac.compare_digest.
  • Never invent your own scheme, never use MD5, SHA-1 or a single SHA-256 for passwords, and enforce a reasonable minimum length instead of arbitrary composition rules.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main purpose of a per-user salt?

    A unique random salt forces the attacker to attack each user separately and makes rainbow tables useless. It does not slow down a single guess and is not secret.
  2. 2. Why is SHA-256(password) unsuitable for storing passwords?

    Hash functions are designed to be fast. For low-entropy human passwords that speed lets attackers try enormous numbers of candidates, so a deliberately slow function is needed.
  3. 3. What extra protection do scrypt and Argon2 offer compared with PBKDF2?

    Memory-hardness makes each guess occupy lots of RAM, so GPUs and ASICs cannot run as many guesses in parallel as they can for CPU-only PBKDF2.
  4. 4. Where should a pepper be stored?

    A pepper only helps if an attacker who steals the database does not also get the pepper. Keeping it in a separate system provides that separation.

Summary

  • Store passwords with a slow, salted, purpose-built hash, never plaintext, never a plain fast hash.
  • A random salt per user defeats rainbow tables and hides repeated passwords; a work factor makes each guess expensive.
  • PBKDF2 costs time only; scrypt and Argon2id also cost memory. Prefer Argon2id for new systems.
  • Store algorithm, parameters and salt with the hash so you can raise the cost later, and compare in constant time.
  • A pepper kept outside the database adds a further layer.

Next: Key Derivation and Key Management, where the same ideas turn passwords and master secrets into encryption keys.