If your application lets people log in with a password, you are holding something valuable that is not really yours. Many people reuse passwords across sites, so a password leaked from your database may open their email, their bank or their work accounts. Databases do leak — through bugs, misconfigured backups, stolen credentials or insiders — so the question is not only how to prevent a breach, but what an attacker gets if one happens.

Stored correctly, passwords should be extremely expensive for an attacker to recover even with a full copy of your database. Stored badly, they can be recovered in bulk within hours. The difference comes down to a few decisions that are easy to get right and surprisingly common to get wrong.

Never store passwords, and never encrypt them

Storing passwords in plain text is the obvious mistake. Less obvious is that encrypting them is also wrong for this purpose. Encryption is reversible by design: anyone with the key can recover every password, and the key usually lives somewhere near the data. If an attacker gets the database, there is a good chance they get the key too.

What you want is a one-way function: a way to turn a password into a value you can check against later without ever being able to turn that value back into the password. When a user logs in, you apply the same function to what they typed and compare the result with the stored value. That one-way function is a hash — but not just any hash.

Why ordinary hashes are the wrong tool

General-purpose cryptographic hash functions such as SHA-256 are designed to be fast. That is exactly what you want for checking file integrity or signing data, and exactly what you do not want for passwords. An attacker with a leaked database can try guesses at enormous speed, especially with graphics cards built for parallel computation, working through common passwords, dictionaries and variations in bulk.

Older functions such as MD5 and SHA-1 are worse still, but the core problem is speed, not age. A password hash needs to be deliberately slow and, ideally, demanding of memory, so that each guess costs an attacker real time and money while a single legitimate login remains fast enough for a user not to notice.

The functions designed for the job

Several algorithms exist specifically for password hashing. Argon2, and in particular the Argon2id variant, won the Password Hashing Competition in 2015 and is widely recommended today, including by the OWASP guidance on password storage. bcrypt is older, extremely widely deployed and still a reasonable choice. scrypt is another memory-hard option, and PBKDF2 with a high iteration count is used where particular compliance standards require it.

All of these share a crucial feature: a configurable cost. You choose how much time and memory each hash takes, and you can raise the cost as hardware improves. The practical advice is to use a well-maintained library for your language that implements one of these algorithms, follow its current recommended parameters, and avoid writing any part of the cryptography yourself.

Salts: why identical passwords must not look identical

If two users choose the same password and you hash it the same way, their stored hashes are identical, and an attacker who cracks one has cracked both. Worse, attackers can precompute hashes of common passwords once and reuse them against every database that hashes the same way.

A salt solves both problems. It is a random value generated separately for each password and combined with it before hashing. The salt is not secret; it is stored alongside the hash. Because every user has a different salt, identical passwords produce different hashes, and precomputed tables become useless. Modern password hashing libraries generate and store salts automatically, embedding them in the output string, which is one more reason to use them rather than assembling your own scheme.

Peppers: an optional extra secret

A pepper is a secret value added to every password before hashing, stored separately from the database — for example in a secrets manager or a hardware security module. If an attacker steals the database but not the pepper, the hashes are far harder to attack.

Peppers add protection but also complexity: you must keep the secret safe, available to your application and backed up, and plan how to rotate it. Many teams treat a pepper as a useful extra layer once the basics — a proper password hashing function with unique salts — are firmly in place, never as a substitute for them.

Upgrading old hashes without forcing resets

Systems often inherit weaker hashes from an earlier era. You cannot simply rehash the stored values with a better algorithm, because you do not know the original passwords. But you can upgrade gradually: when a user logs in successfully, you have their password in hand for a moment, so you can verify it against the old hash, then rehash it with the current algorithm and parameters and store the new value.

Many libraries offer a function that tells you whether a stored hash uses outdated parameters, which makes this pattern easy to implement. For accounts that never log in, some teams wrap the old hash inside a new, stronger hash as an interim measure, and plan to force a reset for remaining legacy accounts.

Details that matter in practice

Comparisons between hashes should be done with the library’s verification function, which uses constant-time comparison so that response timing does not leak information. bcrypt only uses the first 72 bytes of input, which matters for very long passphrases and for schemes that pre-combine a password with other data; the library documentation will describe how it handles this.

Never log passwords, even accidentally — check request logging, error reports and debugging output. Do not impose low maximum password lengths or forbid special characters, which suggest you are storing passwords in a way that cares about their content. And make sure password reset tokens are random, single-use, short-lived and themselves stored hashed.

Hashing is one layer of several

Good hashing limits the damage of a database leak, but it does not stop online guessing against your login form. Rate limiting, detection of credential-stuffing patterns, checks against lists of known breached passwords at registration, and multi-factor authentication all address that side of the problem.

Increasingly, applications also offer passkeys, which remove shared secrets from the server altogether: the server stores a public key and the private key never leaves the user’s device. Where passwords remain, storing them properly is the baseline everything else builds on.

Where the rule stops applying

Password hashing is for secrets that users choose and that you only need to verify, never read back. API keys and tokens you generate yourself are different: they are long and random, so a fast hash such as SHA-256 is usually acceptable for storing them, because brute-forcing a long random value is infeasible regardless of speed.

And some secrets genuinely must be recoverable — credentials your application uses to call another service, for example. Those belong in a secrets manager and may need encryption, not hashing. The rule is precise: human-chosen passwords that only need verification get slow, salted password hashing; other secrets need the tool that fits their purpose.

Choosing the cost parameters

The cost settings decide how slow each hash is, and they are a balance. Too low, and attackers can guess quickly; too high, and logins become slow and a burst of login attempts can overload your servers. A common approach is to choose parameters so that a single hash takes a noticeable fraction of a second on your production hardware, then measure it there rather than on a fast development machine.

Memory-hard algorithms such as Argon2id also let you choose how much memory each hash uses, which makes specialised cracking hardware much less effective. Whatever you choose, record the parameters, review them periodically and raise them as hardware gets faster; the upgrade-at-login pattern lets you do that without disrupting users.

Testing that it works

Password storage is easy to get subtly wrong and hard to notice, because logins still work either way. Add tests that check the stored value never equals the password, that two users with the same password get different stored values, that verification succeeds for the right password and fails for a wrong one, and that the stored format identifies the expected algorithm and parameters.

It is also worth checking, once, what actually ends up in your logs, error reports and analytics during sign-up, login and password reset. Many real leaks come not from the password table but from a forgotten debug log that recorded request bodies.

In short

Never store or encrypt passwords; hash them with a function built for passwords — Argon2id, bcrypt, scrypt or, where required, PBKDF2 — through a maintained library, with a unique salt per password and a cost tuned to current hardware.

Verify with the library’s functions, upgrade old hashes at login, keep passwords out of logs, and add rate limiting, breached-password checks and multi-factor authentication around the login. Done properly, a stolen database becomes an expensive dead end for an attacker rather than a list of your users’ secrets.