Password Storage: Hashing, Salting and Cost
Passwords must be hashed with a slow, salted algorithm. Learn why bcrypt, scrypt and Argon2 are the right choices and what to avoid.
By Velloreth
The rule
Never store a password in a form that can be reversed. Not plain text, not encrypted, not with a fast hash. Store a hash produced by an algorithm that is deliberately slow, with a unique salt per password.
The reason is the threat model. A password database will eventually leak, and the question is not whether but how much damage the leak causes. A slow, salted hash turns a leak from “every account is compromised” into “the weakest passwords are compromised after significant effort”.
Why fast hashes fail
MD5, SHA-1 and SHA-256 are designed for speed and throughput. That is the right property for a checksum and the wrong property for a password.
A single modern GPU can compute on the order of billions of SHA-256 hashes per second. A password list of ten million common passwords can be tested against a leaked hash in a fraction of a second. Even a long password falls quickly if it appears in a wordlist with a few common mutations.
A password hash must be slow. The cost is paid once per login by the legitimate user, and once per guess by the attacker. Making it slow shifts the economics decisively toward the defender.
The three algorithms that are acceptable
bcrypt has been the default for over two decades. It is well tested, widely available, and it has a tunable cost factor. Its main limitation is a 72-byte input limit, which means a very long passphrase is truncated unless it is pre-hashed.
import bcrypt from 'bcrypt';
const hash = await bcrypt.hash(password, 12);
const ok = await bcrypt.compare(password, hash);
scrypt is memory-hard, which means it resists the parallel hardware that makes bcrypt cheaper to attack. It takes parameters for cost, block size and parallelism.
import { scrypt, randomBytes, timingSafeEqual } from 'crypto';
const salt = randomBytes(16);
scrypt(password, salt, 64, { N: 16384, r: 8, p: 1 }, (err, key) => {
// store salt and key
});
Argon2id is the current OWASP recommendation. It is memory-hard, it has a tunable cost, and it won the Password Hashing Competition. The id variant is the recommended one because it resists both side-channel and GPU attacks.
import argon2 from 'argon2';
const hash = await argon2.hash(password, {
type: argon2.argon2id,
memoryCost: 19456,
timeCost: 2,
parallelism: 1,
});
const ok = await argon2.verify(hash, password);
Salting
A salt is a random value, unique per password, stored alongside the hash. It is mixed into the computation so that the same password produces a different hash for every user.
What a salt defeats: a precomputed table of hashes for common passwords, and a rainbow table. Without a salt, an attacker computes the hash of every candidate once and looks it up. With a salt, the attacker must recompute for every user, which multiplies the work by the number of users.
What a salt does not do: it does not make a weak password strong. A salt raises the cost of a bulk attack; it does not save an account whose password is password123.
The salt does not need to be secret. It is stored with the hash, and its value is uniqueness rather than secrecy. Modern libraries generate and store it for you, usually as part of the hash string.
Choosing the cost
The cost must be high enough to be expensive for an attacker and low enough that a login is not slow for a user. The practical target is 100 to 500 milliseconds per hash on production hardware.
Measure, do not copy. A cost of 12 on a laptop may be 12 on a server and 10 on a container with a CPU limit. Benchmark on the machine that will run it, and pick the highest cost that stays within the budget.
Raise it over time. Hardware gets faster, so a cost that was adequate five years ago is cheaper to attack now. Store the parameters with the hash so an old hash can be verified with its original cost and re-hashed with the new one on the next successful login.
Verifying safely
Use a constant-time comparison. A comparison that returns early on the first differing byte leaks information through timing. Every serious library provides a safe comparison, and it should be used rather than ===.
Return a uniform error. A message that distinguishes “no such user” from “wrong password” lets an attacker enumerate accounts. Return the same error for both, and take a similar amount of time.
Rate limit the login endpoint. A slow hash raises the cost of each guess, and a rate limit caps the number of guesses. The two together are what make an online attack impractical.
Common mistakes
Using MD5 or SHA-256. Fast hashes are the wrong tool. This is the single most common and most damaging mistake.
Using a global salt. A single salt for the whole database means two users with the same password have the same hash, and one precomputation covers every account.
A cost that is too low. A cost of 4 is fast to verify and fast to attack. Tune it to the target time.
Storing the password in a log. A request logger that records the body will capture the password. Redact it before logging.
Emailing a password. Email is not a secure channel. Send a reset link with a single-use token instead.
Not invalidating sessions on a password change. A password change should log out every existing session, because the reason for the change may be that an attacker has one.
A quotable summary
A password must be stored as a hash from a deliberately slow, salted algorithm: Argon2id, bcrypt or scrypt. Fast hashes such as MD5 and SHA-256 are unsuitable because a GPU can test billions of candidates per second. The salt is unique per password and defeats precomputed tables, and the cost should be tuned so a single hash takes 100 to 500 milliseconds on production hardware. Verification must use a constant-time comparison and a uniform error message, and a password change must invalidate every existing session.
Related reading
For the authentication flow around this, see session-vs-token-auth and oauth2-explained. For the broader context, secret-management-guide and input-validation-guide.