Password storage
We learn how to store passwords so that a stolen database does not hand over the accounts. Databases leak through backups, SQL injection and insiders, and many users reuse passwords elsewhere. We will see why fast hashes fail, what salts and peppers add, and how bcrypt, scrypt and Argon2 make each guess expensive.
#Section 8Why SHA-256 isn’t a password hash
Assume Wren’s users table leaks. That is the right assumption to design for, since breaches of exactly this kind happen to large companies every year, through backups, injection bugs and insiders. LinkedIn lost 6.5 million unsalted SHA-1 password hashes in 2012, and most were cracked within days. So the question is what the attacker can do with the table.
Suppose Wren stored sha256(password) for each user. The attacker cannot reverse SHA-256, but they do not need to. They guess. Take every password of eight lowercase letters and digits, which is candidates. Hash each one and compare. A single modern GPU computes on the order of SHA-256 hashes per second (an illustrative figure), so it covers the whole space in about 282 seconds. Every user with such a password is exposed in under five minutes, and real attackers do better by trying likely passwords first, from leaked lists and common patterns.
General-purpose hashes are designed to be fast, which is exactly wrong for this job. A password hash, also called a password-based key derivation function, is deliberately slow and has a tunable cost. At an illustrative bcrypt hashes per second, the same search takes seconds, about 8.9 years. The user pays 250 ms once at login and never notices. The attacker pays it on every one of the trillions of guesses.
Unsalted fast hashes have a second flaw. An attacker can hash common passwords once in advance and store the results in a lookup table, or a compressed version called a rainbow table. A leaked table is then matched instantly, without any new hashing. Identical passwords also produce identical hashes, so cracking one cracks every user who shares it, and the most common hash in the table tells the attacker which password to crack first.
#Section 9Salt and pepper
A salt is a random value generated for each user, typically 16 bytes, and stored next to the hash in plain sight. The stored hash becomes . Two users who both chose hunter2 now have unrelated hashes, because their salts differ. A precomputed table is useless, since the attacker would need a separate table for every possible salt. Salts are not secret, and they do not need to be. Their job is uniqueness. They force the attacker to attack each user separately, which multiplies the work by the number of users, a factor of 1,000,000 for Wren.
A pepper is a secret key applied to every password and kept outside the database, in a key management service or a hardware security module. A common construction is . An attacker who steals only a database dump, through SQL injection or a stolen backup, cannot test a single guess without the pepper, so the dump is useless on its own. It does nothing against an attacker who has fully compromised the application server, since the server must use the pepper to check logins.
The pepper's costs are operational. Rotating it requires re-hashing each password at the user's next login, since nobody knows the passwords to re-hash them in bulk, so the system must support two peppers during the change and record which one each hash used. Losing it locks every user out permanently. Many teams skip the pepper and rely on a strong password hash plus tight database access control, and that is a reasonable choice, but if you add one, keep it in a KMS, never in the same database or the same config file as the hashes.
#Section 10bcrypt, scrypt and Argon2
Three algorithms dominate. bcrypt (Provos and Mazières, 1999) makes each guess cost CPU time. Its cost parameter sets the number of rounds of its expensive key setup to , so cost 12 means 4,096 rounds. It is battle-tested and available in every language. It has one sharp edge. It silently ignores input beyond 72 bytes, so two long passphrases that share their first 72 bytes hash identically, and systems that pre-hash to work around this must do it carefully.
scrypt (Percival, 2009) adds a large memory requirement. Attackers crack passwords on GPUs and custom chips that run thousands of small cores in parallel, each with little memory. A hash that needs many megabytes of fast memory per guess limits how many guesses run at once, which cuts the attacker’s advantage far more than CPU cost alone. Argon2 won the Password Hashing Competition in 2015. Its Argon2id variant has separate settings for memory, time (passes over memory) and parallelism (lanes), and it resists both GPU attacks and side-channel attacks. OWASP’s current minimum recommendation is Argon2id with 19 MiB of memory, 2 passes and 1 lane, and it is Wren’s choice.
The work factor is the setting that makes hashing slow. For bcrypt, each step up in cost doubles the time, for the server and for the attacker alike. With illustrative timings, cost 10 takes 62.5 ms, cost 11 takes 125 ms, cost 12 takes 250 ms and cost 13 takes 500 ms. At Wren’s peak of 20 logins per second and 250 ms per hash, hashing alone needs 20 × 0.25 = 5 CPU cores. During a promotion that triples logins, it needs 15. The right setting is the most expensive one your login traffic can afford. Too low helps attackers, too high lets a burst of login attempts exhaust your CPU, which an attacker can do on purpose.
The stored string records everything needed to verify it: algorithm, version, parameters, salt and hash, as in $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>. When Wren raises its parameters, it cannot re-hash stored values, because it never knew the passwords. Instead, after each successful login, it compares the stored parameters with the current target and, if they are weaker, re-hashes the password it just verified. Active users are upgraded within weeks. Accounts that never log in keep old hashes, and some teams force a reset on accounts still using a weak scheme after a deadline.