Pointer Walk

How Passwords Should Be Stored

Add a salt, run a slow hash, store the result. The site never needs to know your password to check it.

SecurityBeginner8 min read

Assume the database will be stolen

Every site that has logins has a table of users. Sooner or later, some site's table gets copied by someone who should not have it: through a bug, a leaked backup, or a careless employee.

So the right question is not "how do we keep the table secret?". It is: when the table is stolen, what does the thief get?

The answer depends entirely on how the passwords were stored. Switch between the three ways.

A stolen users tableInteractive
Stored as
userpassword
anasunshine
bensunshine
cy123456
deeV9!kq#Lm2x

What the thief learns: Every password is readable at once. People reuse passwords, so the thief now holds keys to their email and bank as well.

Way 1: plain text

Store the password as typed. A thief reads every password directly.

It is worse than it sounds, because people reuse passwords. A leak from one small forum hands over the keys to its users' email and bank accounts too. And every employee with database access can read them as well.

Never do this.

Way 2: a hash

A hash function takes any input and produces a fixed-length scramble, the hash.

  • The same input always gives the same hash.
  • A tiny change in the input gives a completely different hash.
  • There is no way to run it backwards from the hash to the input.

So store the hash, not the password. At login, hash what was typed and compare.

This is far better, and still not enough. Look at the table in the demo: ana and ben chose the same password, so they have the same hash. Two problems follow.

  1. Identical passwords are visible

    Crack one and you have cracked everyone who chose it.

  2. Hashes can be worked out in advance

    An attacker hashes the ten million most common passwords once, making a lookup table. Then every stolen hash is checked against it instantly. "123456" is found before you finish reading this sentence.

  3. Ordinary hash functions are fast

    They were designed for speed. Modern hardware computes billions per second, so an attacker can try billions of guesses per second against your table.

Way 3: salt, and a slow hash

Two additions fix those problems.

A salt. When a user signs up, generate a random string just for them. Hash the salt and password together, and store the salt next to the hash. The salt is not secret.

Now ana and ben have different salts, so their hashes differ even though their passwords are the same. And a precomputed table is useless: the attacker would need a separate table for every possible salt.

A slow hash. Use a function designed for passwords, which is deliberately expensive: it takes perhaps a tenth of a second and a chunk of memory per attempt. A real user logging in does not notice a tenth of a second. An attacker who needs billions of attempts certainly does.

Example

What slow buys you

A fast hash at 10 billion guesses per second tries every 8-character password of letters and digits in about 6 hours.

A password hash at 10 guesses per second per user would need about 700,000 years for the same search.

Same password, same attacker. The only change is which function did the hashing.

Checking a login

How can a site check your password if it never stored it?

Checking a password without storing itInteractive
You
The website
Its database

A login, on a site that stores passwords properly.

It repeats the recipe. Take the stored salt, combine it with what you just typed, run the same slow hash, and compare the result with the stored hash. Identical input gives an identical hash, so a match means you typed the right password.

The site never needs to know your password. It only needs to recognise it.

python
from argon2 import PasswordHasher

hasher = PasswordHasher()

# At sign-up: generates a salt and stores it inside the result
stored = hasher.hash("sunshine")

# At login: reads the salt back out, hashes again, compares
hasher.verify(stored, "sunshine")      # passes
hasher.verify(stored, "sunshin3")      # raises an error

What to use

UseDo not use
Argon2id, scrypt or bcrypt, from a well-known libraryPlain text
A unique random salt per user (the libraries do this for you)Reversible encryption: whoever gets the key gets every password
A cost setting high enough to take around a tenth of a secondFast general-purpose hashes such as MD5, SHA-1 or plain SHA-256
A function you wrote yourself

What this does not fix

Salted, slow hashing protects passwords after the table is stolen. It does not help against a weak password guessed at the login form, a password phished from the user, or one reused from another site that leaked it in plain text. Rate-limited logins, two-factor authentication and long passwords cover those. See Password Length and Brute Force.

Key takeaways

  • Plan for the users table being stolen, and ask what the thief gets.
  • Never store passwords in plain text or with reversible encryption.
  • A hash cannot be reversed, but plain fast hashes are easy to attack with precomputed tables and sheer speed.
  • A unique salt per user makes identical passwords look different and defeats precomputed tables.
  • A deliberately slow password hash (Argon2id, scrypt, bcrypt) makes each guess expensive.
  • A login is checked by hashing the typed password with the stored salt and comparing.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress