Password hashing: salt and Argon2
One table, four ways to store it
A site does not need to know your password: at login it only checks whether the typed password gives the same
record as the one in its database. That is why passwords are hashed, not encrypted — anyone who gets the key, which
lives somewhere on the same server, can decrypt a ciphertext, while a hash function cannot be reversed. A fast hash
alone is not enough, though. Without salt the same password always gives the same hash, so whoever holds the table
sees at once who shares a password. A salt — 16 random bytes, different for every account, stored in the clear next
to the hash — gives equal passwords different records. The last ingredient is cost: PBKDF2 repeats HMAC-SHA-256
600,000 times, and Argon2id fills and reads 19 MiB of memory for every check. These are the minimum parameters
from the OWASP recommendations.
PBKDF2 is computed by WebCrypto; WebCrypto has no Argon2id, so the @noble/hashes library (pure JavaScript)
computes it, checked against the RFC 9106 test vectors.
A site’s password table
Type any password, just not a real one of yours. Everything is computed in the browser; nothing goes to a server.
The password must have 1 to 128 B.
SHA-256(password) — a fast hash without salt.
Computing SHA-256: 0 of 7 accounts stored.
- alicja password: qwerty123
computing…
- bartek password: zaq12wsx
computing…
- celina password: qwerty123
computing…
- darek password: lato2024
computing…
- ewa password: qwerty123
computing…
- filip password: wydra-kompas-17-rabarbar
computing…
- you password: zaq12wsx
computing…
- One password check on this device
- measuring…
- Groups of identical records
- …
The server pays this on every login — and the attacker pays the same for every guess at a leaked password (Break it, below).
A dictionary, a table and a graphics card
Databases leak, so assume the attacker holds the table: logins, salts and hashes. A hash cannot be reversed, but it can be guessed: hash a common password and compare. Without salt, one hash of a word checks every account at once, and the hashes of a whole dictionary can be computed once, before the leak, and then only looked up — in every later unsalted database. Rainbow tables (Philippe Oechslin, 2003) are a thriftier version of that table: instead of every password–hash pair they keep only the start and end of long chains in which a hash is turned into the next candidate password, and pay for it with computation during the lookup. The toy shows a plain table, because a salt disables both the same way: nothing can be computed in advance for a salt nobody knows yet.
Leaked database
Dictionary: 100 passwords written for this site — runs of digits, keyboard walks, words with digits, a season with a year. Real dictionaries are passwords from earlier leaks and are far longer.
Loading the leak…
- Words tried
- 0 of 100
- Hashes computed during the attack
- 0
- Cracked accounts
- 0 of 0
Salt does not save a weak password — it only makes every account a separate job. What is left is guessing speed. A graphics card computes thousands of hashes in parallel, so a fast function such as SHA-256 or MD5 loses to it, salted or not. PBKDF2 multiplies the cost of one guess by the iteration count, but each iteration is still a small circuit that the card replicates thousands of times. Argon2id is memory-hard: every guess holds its own 19 MiB for the whole computation, and from the middle of the first pass on, its memory reads depend on the data. In hardware, memory takes chip area just like computing cores, so every parallel guess — on a graphics card or in an ASIC — costs its own 19 MiB. RFC 9106 measures the cost of an attack by exactly this time–area product; the best known attack on multi-pass Argon2id that saves memory at the cost of recomputation reduces it only by a factor of 1.33.
Guessing speed
Measure how many guesses per second each way of storing allows on this device: real salted computations, one after another.
SHA-256 runs for about 0.3 s, PBKDF2 and Argon2id for at least two full computations each. The page keeps working meanwhile.
Press “Measure”: three measurements, one after another.
- Salted SHA-256 one hash per guess
- Guesses per second (measured)
- —
- PBKDF2-HMAC-SHA-256 600,000 iterations of HMAC-SHA-256 per guess
- Guesses per second (measured)
- —
- Argon2id m = 19,456 KiB, t = 2, p = 1 per guess
- Guesses per second (measured)
- —
Salt does not slow a single guess: salted SHA-256 is almost as fast as unsalted. Salt makes the attacker go account by account; only a deliberately costly function slows each guess.
- Salted SHA-256
- ≈ 19,500,000,000 guesses/s
- Salted SHA-256: 26⁸ on this card
- ≈ 11 seconds
- PBKDF2-HMAC-SHA-256
- ≈ 14,800 guesses/s (extrapolated from 999 iterations)
- PBKDF2-HMAC-SHA-256: 26⁸ on this card
- ≈ 160 days
- Argon2id
- not measured: hashcat 6.2.6 has no Argon2 mode
Argon2id: every guess holds its own 19,456 KiB of memory for the whole computation, so the card’s 24,563 MiB holds at most about 1,292 guesses at once. SHA-256 needs a few hundred bytes of state per guess — there, the card’s memory limits nothing.
The card’s numbers are a hashcat 6.2.6 benchmark of an RTX 4090 at stock clocks, published by Chick3nman in October 2022 — not a measurement on your device. Salted SHA-256 is mode sha256($salt.$pass), the same record as in the toy. PBKDF2 runs 999 iterations in the benchmark; the cost of a guess grows linearly with the iteration count, so the result is scaled to 600,000. RTX 4090 benchmark (hashcat)
Leaks and slow functions in practice
- RockYou (2009)A trivial SQL injection leaked the site’s database of over 32 million accounts. The passwords were stored in plain text, so nothing had to be cracked.Source (RockYou (2009))
- LinkedIn (2012)Passwords were stored as SHA-1 without salt. The data from the 2012 breach surfaced in 2016 — 164 million email addresses with passwords — and the vast majority of the passwords were cracked within days of the release.Source (LinkedIn (2012))
- Adobe (2013)In a leak of about 153 million records the passwords were encrypted instead of hashed: the same password gave the same ciphertext, and next to it lay plain-text password hints that gave a lot away.Source (Adobe (2013))
- Password Hashing Competition (2015)An open competition for a password hashing function picked Argon2, by Alex Biryukov, Daniel Dinu and Dmitry Khovratovich, in July 2015. Catena, Lyra2, Makwa and yescrypt received special recognition. Argon2 is now specified in RFC 9106 (2021).Source (Password Hashing Competition (2015))
- BitwardenThe password manager derives the key from the master password with PBKDF2-SHA-256 and 600,000 iterations by default, as OWASP recommends. Argon2id is also available, by default with 32 MiB of memory, 6 iterations and 4 threads.Source (Bitwarden)
Argon2id from a library, rehashed at login
Hash passwords with Argon2id from a library that draws the salt itself and stores the parameters together with the
hash in one string (PHC format). The OWASP minimum is m = 19 MiB, t = 2, p = 1;
RFC 9106 advises taking as much memory and time as the server
can afford at login — measure it on yours. On every successful login, check whether the record has the current
parameters, and if not, recompute it from the password that just arrived. Compare hashes in constant time — the
libraries’ verify functions do. Where Argon2id is not available, OWASP recommends scrypt (N = 2¹⁷, r = 8,
p = 1), PBKDF2-HMAC-SHA-256 with 600,000 iterations where FIPS-140 is required, and bcrypt (cost ≥ 10, password up
to 72 B) only in legacy systems. As an extra layer you can add a
pepper: a secret
key shared by all passwords and kept outside the database (in a secrets vault or an HSM), so a database leak alone
is not enough to start guessing.
import * as argon2 from 'argon2'; // node-argon2, on the server
// OWASP minimum: 19 MiB, 2 passes, 1 lane; the library draws a 16 B salt
const params = {
type: argon2.argon2id,
memoryCost: 19456,
timeCost: 2,
parallelism: 1,
};
const stored = await argon2.hash(password, params);
// '$argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>'
// login: verify compares in constant time
if (await argon2.verify(stored, attempt)) {
if (argon2.needsRehash(stored, params)) {
user.passwordHash = await argon2.hash(attempt, params);
}
}.NET has no Argon2id in its standard library. ASP.NET Core Identity uses PBKDF2-HMAC-SHA-512 and reports a needed rehash by itself once you raise the iteration count — raise the default 100,000 to the 220,000 OWASP recommends.
var hasher = new PasswordHasher<User>(Options.Create(
new PasswordHasherOptions { IterationCount = 220_000 }));
user.PasswordHash = hasher.HashPassword(user, password);
// login: constant-time comparison, rehash on outdated parameters
var result = hasher.VerifyHashedPassword(user, user.PasswordHash, attempt);
if (result == PasswordVerificationResult.SuccessRehashNeeded)
user.PasswordHash = hasher.HashPassword(user, attempt);Do not store passwords in plain text or encrypted — like RockYou and Adobe. Do not use fast hashes (MD5, SHA-1,
SHA-256), not even with a salt: one graphics card tries billions of them per second. Do not give every account the
same salt, do not draw it with Math.random(), and do not compare hashes with a loop or === that stops at the
first differing byte. A key from high-entropy material, such as an ECDH secret, is derived with HKDF, not with a
password function (the HKDF station). If the server should never see the password at all,
use the OPAQUE protocol: the browser blinds the password before sending anything, and the
client runs Argon2id.
// ✕ fast hash without salt: same password, same hash
const digest = new Uint8Array(await crypto.subtle.digest(
'SHA-256',
new TextEncoder().encode(password),
));
// ✕ comparison that stops at the first differing byte
const same = digest.every((b, i) => b === stored[i]);// ✕ a salt does not slow SHA-256: still one fast hash per guess
byte[] hash = SHA256.HashData(
salt.Concat(Encoding.UTF8.GetBytes(password)).ToArray());