Why you hash passwords instead of storing or encrypting them
You never need to know a user's password, only to check it. So store a one-way hash: at login, hash the password the user typed and compare it with the stored value. If the database leaks, the attacker gets hashes, not passwords.
Encryption is the wrong tool because it is reversible by design. Whoever holds the key can decrypt every password, and the key has to be available to the application, so it tends to leak along with the data. A site that can email you your existing password is storing it reversibly.
Why MD5 and SHA-256 are wrong for passwords
They are fast, and speed helps the attacker. A stolen hash can't be reversed, but it can be guessed: hash a candidate password, compare, repeat. A fast hash makes each guess cheap.
We measured the difference on one laptop (Intel Core i7-13700H, Windows 11) on 2026-10-26:
| Function and settings | Time per hash | Guesses per second, one core | How measured |
|---|---|---|---|
| SHA-256, 16-byte input | about 0.07–0.08 µs | 12–15 million | openssl speed -seconds 3 -bytes 16 -evp sha256, 3 runs |
| PBKDF2-HMAC-SHA256, 600,000 iterations | 245–340 ms | about 3–4 | Node.js 18 crypto.pbkdf2Sync, 3 runs |
| scrypt, N=217, r=8, p=1 | 348–396 ms | about 3 | Node.js 18 crypto.scryptSync, 3 runs |
With OWASP's settings, each guess costs millions of times more than a plain SHA-256. A login that takes a third of a second is fine for a user and ruinous for someone testing billions of candidates. Node.js has no built-in Argon2 or bcrypt, so we did not time those. Your numbers will differ by CPU, but the ratio is the point.
An unsalted fast hash has a second weakness: the same password always gives the same hash. Attackers precompute tables of common passwords and their hashes (rainbow tables are a compact form of these), so cracking becomes a lookup. On every unsalted system, 5f4dcc3b5aa765d61d8327deb882cf99 is the MD5 of password, so cracking it once cracks it for every user who chose it.
What a salt is and where to store it
A salt is a random value generated for each password and combined with it before hashing. Two users with the same password get different stored hashes, which makes precomputed tables useless and hides which accounts share a password.
The salt is not a secret. Store it next to the hash; modern libraries put it inside the hash string for you. NIST SP 800-63B requires a salt of at least 32 bits, and bcrypt always uses a 128-bit salt. Generate it with a cryptographically secure random source, or let the library do it.
Which password hashing algorithm to use, with parameters
OWASP's Password Storage Cheat Sheet gives these minimum settings. They were current when we checked on 2026-10-26; OWASP raises them as hardware gets faster, so check the sheet before you deploy.
| Algorithm | OWASP minimum settings | When to use it |
|---|---|---|
| Argon2id | 19 MiB memory, 2 iterations, parallelism 1 | First choice for new systems |
| scrypt | N=217 (CPU/memory cost), r=8, p=1 | When Argon2id is not available |
| bcrypt | Work factor 10 or more | Legacy systems; input limited to 72 bytes |
| PBKDF2-HMAC-SHA256 | 600,000 iterations | When FIPS-140 compliance is required |
| MD5, SHA-1, SHA-256 alone | None | Never for passwords |
Argon2id and scrypt are memory-hard: each hash needs a fixed amount of RAM, which limits how many guesses a GPU or custom chip can run in parallel. PBKDF2 is not memory-hard, which is why it needs so many iterations. bcrypt ignores everything after the first 72 bytes of input, so a very long passphrase is effectively truncated.
Use a maintained library for your language rather than writing any of this yourself. Call its hash function at sign-up and its verify function at login. Store the single string it returns, which records the algorithm, the parameters and the salt, so you can raise the cost later without breaking existing hashes.
What a pepper adds
A pepper is a secret mixed into every password before hashing. Unlike a salt, it is the same for all users and it must not be stored with the hashes: keep it in a secrets manager or hardware security module, not in the database. An attacker who steals only the database then cannot test guesses at all. It is an extra layer on top of salting, and it brings key storage and rotation work with it.
How to upgrade old MD5 or SHA-256 password hashes
You can't re-hash a password you don't have, so there are two ways to migrate:
- On next login. When a user logs in successfully, you briefly have the plaintext: hash it with the new algorithm and replace the old hash. Accounts that never log in keep the weak hash, so set a cut-off after which they must reset their password.
- Wrap the old hash now. Treat each stored MD5 or SHA-256 value as the input to the new function, for example Argon2id(MD5(password)), and mark those rows. Every account gets the slow hash immediately; replace the wrapped value with a direct hash at the user's next login.
Other parts of safe password handling
- Length rules. NIST SP 800-63B (revision 4) requires at least 15 characters for passwords used on their own and at least 8 when the password is one part of multi-factor login. It says to allow at least 64 characters and not to impose composition rules such as "one symbol and one digit".
- Rate limiting. Throttle repeated login attempts per account and per IP address so online guessing is slow even before hashing matters.
- Multi-factor authentication. A cracked password alone is then not enough to take over an account.
- Use the library's verify function. Don't compare hash strings yourself: an ordinary string comparison can stop at the first differing character, and that timing difference can leak information.
For users choosing passwords, our password generator creates random passwords and passphrases in the browser.
Frequently asked questions
- Is bcrypt still safe to use?
- Yes, with a work factor of 10 or more, which is OWASP's minimum. OWASP now lists it after Argon2id and scrypt, for legacy systems. Its main limitation is that it uses only the first 72 bytes of a password, so very long passphrases are effectively truncated.
- Does a pepper replace a salt?
- No. A salt is unique to each password and stored with the hash; it stops precomputed tables and hides shared passwords. A pepper is one secret shared by all passwords and kept outside the database. Use a salt always; add a pepper only if you can store and rotate the secret properly.
- Should passwords be hashed in the browser before they are sent?
- Not instead of server-side hashing. If the server stores whatever the browser sends, the browser-side hash simply becomes the password, and anyone who steals it can log in with it. Send the password over HTTPS and hash it on the server with a slow, salted function.
- How can I tell how a site's passwords are stored?
- Look at the stored value. A string starting with $2b$ is bcrypt and one starting with $argon2id$ is Argon2id; both include their parameters and salt. A bare 32 or 64 character hex string with no parameters is almost certainly a raw MD5 or SHA-256 hash and needs migrating.
Why there are no test vectors on this page
The other guides in this set end with known-answer tests you can reproduce. This one does not, because a correctly stored password hash is not reproducible from the password alone. Each call generates a fresh random salt, so hashing the same password twice must give two different stored values. If you find a system where two users with the same password have identical stored hashes, it is unsalted, and one precomputed table cracks every common password in it.
What you can check is the format of a stored hash. A bcrypt hash such as $2b$12$… records the variant (2b) and the work factor (12) in the string. Argon2 libraries store the variant, version and parameters the same way, as in $argon2id$v=19$m=19456,t=2,p=1$…, followed by the salt and hash. The timing table above is the reproducible part: the commands are listed, so you can measure the cost on your own hardware.
Our hash generator and HMAC tool are right for checksums and message signatures and wrong for storing passwords, because they are fast.
Sources
Recommendations and parameters are taken from the current standards and the original specifications; timings are our own measurement, described above.
- OWASP, Password Storage Cheat Sheet: algorithm order, minimum parameters, the bcrypt 72-byte limit, peppers and upgrading legacy hashes
- NIST SP 800-63B-4, Digital Identity Guidelines: Authentication: password length rules, no composition rules, salt of at least 32 bits
- RFC 9106, Argon2 Memory-Hard Function: the specification of Argon2 and its id variant
- Provos & Mazières (1999), A Future-Adaptable Password Scheme: the paper introducing bcrypt, its 128-bit salt and tunable cost
- RFC 2898, PKCS #5: Password-Based Cryptography: the definition of PBKDF2
This guide is general educational information about defensive password storage, not security advice for a specific system. Follow current guidance from a trusted source such as OWASP and have authentication code reviewed by a qualified professional.