Storing passwords: salt and slow hashes

Introduction

Every website with a login has to store something that lets it check your password. The worst choice is the password itself: one stolen database, and every user's password is in the attacker's hands, along with every other site where they reused it. Large password leaks happen every year, so a careful developer plans for the day their own database is copied.

This lesson builds safe password storage in three steps: hash the password, add a salt, and make the hash slow on purpose.

How it works

Step 1: store a hash, not the password. At sign-up the server stores hash(password). At login it hashes what you typed and compares. The server never needs the password itself, and a hash cannot be reversed.

That alone is not enough. Attackers keep precomputed tables of the hashes of millions of common passwords (a compact version is called a rainbow table). With plain hashes, one look-up per stolen hash reveals every common password at once. Worse, two users with the same password have the same hash, so cracking one cracks both.

Step 2: add a salt. A salt is a random value made for each user when they set a password, stored next to the hash. The server stores hash(salt + password). Now:

  • Precomputed tables are useless: they would need a separate table for every possible salt.
  • Two users with the same password get different hashes.
  • An attacker must attack each user separately, multiplying the work by the number of users.

The salt is not secret. It only has to be unique and random.

Step 3: make it slow. SHA-256 is designed to be fast; a single graphics card can compute billions of hashes per second, so a stolen database of fast hashes can be guessed very quickly. Password hashing algorithms are deliberately slow and tunable: bcrypt, scrypt and Argon2 (the winner of the Password Hashing Competition in 2015, and the one OWASP recommends first). bcrypt has a cost setting: each step up doubles the work, because it runs 2^cost rounds. A login that takes a tenth of a second is not noticed by a user, but it cuts an attacker's guessing speed by many thousands of times.

Example

Two students choose the same password. Watch the salt keep their stored hashes apart. (FNV-1a stands in for a real password hash so the steps are visible; real code would call bcrypt or Argon2.)

// Storing passwords: a random salt per user, and only hash(salt + password) is kept.
// (FNV-1a stands in for a real password hash such as bcrypt; it is far too fast for real use.)
function fnv1a(text) {
  let hash = 2166136261;
  for (let k = 0; k < text.length; k++) {
    hash = hash ^ text.charCodeAt(k);
    hash = Math.imul(hash, 16777619) >>> 0;
  }
  return hash.toString(16).padStart(8, "0");
}

const users = [
  { name: "rahim", salt: "q7Lp", password: "dhaka2024" },
  { name: "anika", salt: "Z2xe", password: "dhaka2024" },
];
const stored = {};
for (const u of users) {
  stored[u.name] = { salt: u.salt, hash: fnv1a(u.salt + u.password) };
  console.log(u.name + " stores salt " + u.salt + ", hash " + stored[u.name].hash);
}

function login(name, password) {
  const record = stored[name];
  return record && fnv1a(record.salt + password) === record.hash ? "welcome" : "wrong name or password";
}
console.log("rahim / dhaka2024: " + login("rahim", "dhaka2024"));
console.log("rahim / dhaka2025: " + login("rahim", "dhaka2025"));
Open in compiler

Line by line

  1. Each user record has a name, a random salt and the password they chose. Rahim and Anika both chose dhaka2024.
  2. The loop stores only the salt and fnv1a(salt + password), never the password.
  3. Their stored hashes, f77e9c1c and d4d27d19, are different, because their salts differ. An attacker cannot see that they share a password.
  4. login looks up the record, hashes the salt with the typed password and compares. The right password matches; one wrong digit does not.
  5. The message wrong name or password does not say which part was wrong, so it does not help an attacker find valid user names.

Expected result

rahim stores salt q7Lp, hash f77e9c1c
anika stores salt Z2xe, hash d4d27d19
rahim / dhaka2024: welcome
rahim / dhaka2025: wrong name or password

In the real world

  • bcrypt's stored string holds everything needed to check a password: the algorithm, the cost and a 128-bit salt, followed by the hash.
  • When computers get faster, sites raise the cost and re-hash each password the next time its owner logs in.
  • Some systems also add a pepper: a secret value kept outside the database, so a stolen database alone is not enough to start guessing.
  • Services such as "Have I Been Pwned" let sites check new passwords against lists of leaked ones without sending the password itself.

Common mistakes

  • Storing passwords encrypted instead of hashed. Whoever gets the key can decrypt every password.
  • Using one salt for everyone. A shared salt still lets one guess be tested against every user at once.
  • Using a fast hash such as MD5 or plain SHA-256 for passwords. They are built for speed, which helps the attacker.
  • Writing your own scheme. Use the password hashing function of a well-known library.

Best practices

  • Use Argon2id, scrypt or bcrypt through a maintained library, with its recommended settings.
  • Generate a new random salt for every password, including on every password change.
  • Tune the cost so one hash takes a noticeable fraction of a second on your server, and raise it over time.
  • Never log passwords, and compare hashes with a constant-time comparison.

Summary

Never store passwords; store a hash of each one, so a stolen database does not reveal them. Add a unique random salt per user, which defeats precomputed tables and stops one guess from being tested against everyone at once. Then use a deliberately slow, tunable password hash such as Argon2, scrypt or bcrypt, so each guess costs the attacker real time. The salt is stored openly; what protects users is the one-way, slow hash.

Quick check

1. What does a per-user salt stop?
2. Must the salt be kept secret?
3. Why is plain SHA-256 a poor choice for storing passwords?
4. A site raises its bcrypt cost from 10 to 12. How much more work is each hash?

Key takeaways

  • Store salted hashes, never passwords and never encrypted passwords.
  • A unique random salt per password defeats precomputed tables and forces per-user attacks.
  • Password hashes must be slow; use Argon2id, scrypt or bcrypt with a tuned cost.
  • The salt is not secret; the protection comes from the one-way, slow hash.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.