learn

How to store passwords safely

Why SHA-256 alone is not enough, what salt, cost and pepper do, and the parameters OWASP recommends for Argon2id, bcrypt, scrypt and PBKDF2 today.

The rule

Store passwords with a slow, salted password-hashing function: Argon2id first, then scrypt, bcrypt or PBKDF2. Never store them in plain text, never encrypt them (whoever gets the key reads them all) and never use a fast hash such as MD5 or plain SHA-256, which a single GPU tests by the billions per second. When a database leaks, the goal is that each password still costs an attacker real time and money to guess.

Why a fast hash is not enough

A fast hash makes guessing cheap. Common passwords like senha123 or 123456 are in every wordlist; with SHA-256 an attacker hashes the whole list and compares it with your leaked table in seconds. Two defenses fix this:

  • Salt: a random value, unique per user, stored next to the hash. Two users with the same password get different hashes, so precomputed tables are useless and each account must be attacked separately.
  • Cost: the function is made deliberately slow (iterations) and, for Argon2 and scrypt, memory-hungry, so each guess costs milliseconds and megabytes instead of nanoseconds.

See hash vs encryption and MD5 vs SHA-256 for the background.

Recommended settings

The OWASP Password Storage Cheat Sheet gives these minimums; raise them as far as your login latency allows (aim for 0.2 to 0.5 s per hash on your server):

AlgorithmMinimum settingsNotes
Argon2id19 MiB memory, 2 iterations, 1 lanefirst choice for new systems
scryptN = 2^17 (128 MiB), r = 8, p = 1memory-hard, widely available
bcryptcost 10 or moreonly the first 72 bytes of the password count
PBKDF2600,000 iterations with HMAC-SHA-256required where FIPS-140 compliance matters

Each algorithm stores its parameters and salt inside the result, for example a bcrypt hash starts with $2b$12$, where 12 is the cost. Try different costs in the bcrypt generator and see how long each one takes, or compute PBKDF2 and Argon2 in the hash generator.

A pepper and other details

  • A pepper is a secret added to every password (for example with HMAC) and kept outside the database, in a secrets manager. A database leak alone then reveals nothing useful.
  • Upgrade gradually: when a user logs in with an old hash, verify it, then rehash the password with the new algorithm and store the new value.
  • Compare hashes with a constant-time function provided by the library.
  • Limit login attempts and offer two-factor authentication; hashing protects leaked data, not the login form. Codes for authenticator apps can be tested in the OTP generator.
  • Encourage long passphrases; the password strength analyser shows how long a guess would take, and the token generator makes random passwords.

FAQ

Is bcrypt still safe?

Yes, with a cost of 10 or more. Its limits are the 72-byte input and low memory use, which is why Argon2id is preferred for new systems.

Should I hash passwords in the browser before sending them?

No, not as a replacement. Send the password over HTTPS and hash it on the server; a hash computed in the browser simply becomes the password.

What happens if I need to raise the cost later?

Store the new parameters with each hash and rehash at the next successful login, as described above. Old hashes keep working until then.

Can I recover a user's password from the hash?

No, and you should not be able to. Offer a reset link instead.