Password storage
| Type | Storage practice for sign-in secrets |
|---|---|
| Also known as | Password hashing |
| Functions in use | Argon2id, scrypt, bcrypt, PBKDF2 |
| Standards | NIST SP 800-63B-4; RFC 9106 (Argon2) |
| CWE | CWE-916: Use of Password Hash With Insufficient Computational Effort |
| OWASP Top 10 | A04:2025 Cryptographic Failures |
A site that accepts passwords has to recognise each one again at every sign-in, and whatever it keeps for that purpose is exposed when the database is copied, through SQL injection, a dump or a leaked backup.1 With correct password storage, the server never keeps the password. It stores the output of a password hashing function, computed from the password, a random salt and a cost setting; at sign-in it runs the same function on the typed password and compares the two results.23
The functions built for this are Argon2id, scrypt, bcrypt and PBKDF2. General-purpose hashes such as MD5, SHA-1 and SHA-256 were designed to run fast, and speed is what an attacker testing guesses needs.13
Hashing and encryption
Encryption, as in Encryption at rest, is reversible by whoever holds the key, so an encrypted password table plus its key gives back every password. A hash has no key and no reverse operation: from the output, the only route to the input is guessing.1 Encrypting passwords is justified only where the application must pass the original on to a system that lacks a modern sign-in method such as OpenID Connect.1
Offline guessing
Once hashes are copied, guessing happens on the attacker's own machines, where no rate limit or lockout applies. Each guess is a candidate password hashed with the stored salt and settings and compared with the stored value.1 With GPUs, specialised chips and rented servers, offline attackers compute many billions of hashes per second.32
A password hashing function raises the price of each guess in two ways: CPU time, through many internal iterations, and memory. Argon2 and scrypt are memory-hard, meaning every computation has to fill a set amount of memory, which makes large parallel attacks on graphics hardware more expensive.345
Salt and pepper
A salt is generated for each password and stored beside its hash in the clear. Because every hash has a different salt, a guess cannot be hashed once and compared with the whole table, and precomputed tables of hashes (rainbow tables) do not apply. The work grows in step with the number of accounts, and two users with the same password get unrelated hashes.1 NIST SP 800-63B-4 sets the minimum salt length at 32 bits; Node.js asks for at least 16 random bytes for scrypt.25 Argon2id and bcrypt libraries, and PHP's password_hash(), generate the salt themselves.16
A pepper is one secret shared by all hashes and kept outside the database, in a secrets vault or a hardware security module. It is mixed into the password before hashing, or used as an HMAC key over the finished hash. A copy of the database alone, from an injection flaw or a backup, is then not enough to start guessing.12 A leaked pepper can only be replaced by having every user reset the password.1
Cost settings
| Function | Minimum settings | Use |
|---|---|---|
| Argon2id | 19 MiB memory, 2 iterations, parallelism 1 (or 46 MiB and 1 iteration) | First choice; won the 2015 Password Hashing Competition |
| scrypt | N=217 (128 MiB), r=8, p=1 | Where Argon2id is unavailable |
| bcrypt | Work factor 10 | Legacy systems; reads at most 72 bytes |
| PBKDF2-HMAC-SHA256 | 600,000 iterations | Where FIPS-140 validated code is required |
The cost should be well above these minimums: as high as the server can bear and raised over time as hardware improves.2 Response time sets the upper bound. A common limit is under one second per hash, and a cost set too high lets a flood of sign-in attempts use up the server's CPU.1
The stored string
PHP's password_hash() writes the algorithm, the cost, the salt and the hash into one string, so password_verify() needs nothing else. It returns strings such as $2y$12$4Umg0rCJ..., where 2y marks bcrypt and 12 is the cost; the manual suggests a 255-character column because the default algorithm may change to one with longer output.6 The PHC string format does the same job for Argon2 and other functions.1 With the scheme and cost stored beside each hash, old and new settings can share one table during a move.21
Supabase Auth stores a bcrypt hash with its own random salt for each user in the encrypted_password column of auth.users. Despite the name, the value is a hash.7
The bcrypt length limit
Most bcrypt implementations read the first 72 bytes of input and ignore the rest; the PHP manual documents the same truncation.16 When other data comes before the password in the input, a long enough prefix pushes the password past that limit. Okta's AD/LDAP delegated authentication built a cache key by hashing one string of user ID, username and password with bcrypt. From a release on 23 July 2024, a user with a username of 52 characters or more could sign in with the username and the cached key of an earlier successful sign-in, provided MFA was off and the cache was consulted first, as happens when the directory agent cannot be reached.8 With a long enough username, the password fell past the bcrypt limit and no longer changed the key.9 Okta found the bug on 30 October 2024 and fixed it that day by switching the cache key from bcrypt to PBKDF2.8
For bcrypt, the usual answer is a 72-byte maximum on passwords. Pre-hashing with plain SHA-512 is no stronger than SHA-512 alone, and raw output can hold a null byte that ends the input early; the safer form is bcrypt over a base64-encoded HMAC keyed with a pepper.1
In code
Node.js includes scrypt in its crypto module; crypto.argon2() was added in version 24.7.0. The OWASP scrypt setting needs 128 MiB per hash, above the default maxmem of 32 MiB, so the limit has to be raised. timingSafeEqual() compares the result in constant time.5
// Node.js, built-in crypto module
import { scrypt, randomBytes, timingSafeEqual } from "node:crypto";
import { promisify } from "node:util";
const scryptAsync = promisify(scrypt);
// N=2^17, r=8 needs 128 MiB, above the 32 MiB default maxmem
const OPTS = { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 };
export async function hashPassword(password) {
const salt = randomBytes(16);
const key = await scryptAsync(password, salt, 32, OPTS);
return "scrypt$17$8$1$" + salt.toString("base64") + "$" + key.toString("base64");
}
export async function verifyPassword(password, stored) {
const [, , , , salt, key] = stored.split("$");
const expected = Buffer.from(key, "base64");
const actual = await scryptAsync(password, Buffer.from(salt, "base64"), expected.length, OPTS);
return timingSafeEqual(actual, expected);
}PHP's password_hash() with PASSWORD_DEFAULT uses bcrypt, at cost 12 since PHP 8.4, and password_needs_rehash() reports when a stored hash was made with older settings.610
// PHP: sign-up
$hash = password_hash($password, PASSWORD_DEFAULT); // store in a 255-character column
// PHP: sign-in
if (password_verify($password, $hash)) {
if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
$hash = password_hash($password, PASSWORD_DEFAULT);
// save the new $hash in place of the old one
}
}Moving off an old scheme
An application holding MD5 or SHA-1 hashes can rehash each password with the new function at the user's next sign-in, when the server briefly has the plain password.1 Hashes of accounts that never return can be expired, with a reset required, or wrapped in the new function, as in bcrypt(md5(password)), and replaced with a direct hash at the next sign-in.1
In SecFlash
SecFlash reports MD5, SHA-1 and other broken algorithms in the source as SF-SEC-707 and in mobile app code as SF-SEC-787, both on the Security page. A response that contains a value shaped like a hash is reported as SF-SEC-643 on the same page, since a password hash in a response can be cracked offline.
See also
- Authentication
- Credential stuffing
- Brute-force attack
- Password spraying
- Passkeys and WebAuthn
- Database dumping
- Multi-factor authentication
References
- a b c d e f g h i j k l m n o p q "Password Storage Cheat Sheet". OWASP Cheat Sheet Series.
- a b c d e f National Institute of Standards and Technology (2025). "Digital Identity Guidelines: Authentication and Authenticator Management". NIST SP 800-63B-4.
- a b c d "CWE-916: Use of Password Hash With Insufficient Computational Effort". Common Weakness Enumeration. MITRE.
- back Biryukov, A., Dinu, D., Khovratovich, D. and Josefsson, S. (September 2021). "Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications". RFC 9106.
- a b c Node.js. "Crypto". Node.js documentation.
- a b c d The PHP Group. "password_hash". PHP Manual.
- back Supabase. "Password security". Supabase Docs.
- a b Okta (1 November 2024, updated 4 November 2024). "Okta AD/LDAP Delegated Authentication: Username Above 52 Characters". Okta Security Advisories.
- back Jones, C. (4 November 2024). "Why the long name? Okta discloses auth bypass bug affecting 52-character usernames". The Register.
- back The PHP Group. "password_needs_rehash". PHP Manual.
External links
- Password Storage Cheat Sheet, OWASP Cheat Sheet Series