Cybersecurity

How Websites Should Store Passwords: Hashing and Salting

See how a well-built site checks your password without keeping it, and what salts, slow hashes and peppers each protect against.

Key takeaways

  • A password should be stored as a salted hash produced by a deliberately slow password-hashing function such as Argon2id, scrypt, bcrypt or PBKDF2.
  • A unique random salt per account makes precomputed tables useless and forces an attacker to guess each account separately.
  • Fast general-purpose hashes such as MD5, SHA-1 or a single round of SHA-256 are unsuitable for passwords, even with a salt.
  • Hashing limits the damage from a stolen database, but weak and reused passwords can still be guessed.
  • A site that can email you your existing password is storing it in a recoverable form, which is a serious design flaw.

A well-built website never stores your password. It stores the output of a one-way function, called a hash, computed from your password plus a random value unique to your account, called a salt. When you sign in, the site repeats the calculation and compares results. If someone steals the database, they get hashes they have to guess against, one account at a time.

The details matter a great deal. The choice of hashing algorithm and its settings decide whether a stolen database is cracked in an afternoon or stays mostly unreadable. This article explains how the pieces work, what a good design looks like, and what you can infer about a site from the way it behaves.

Why the password itself should never be kept

A database can leak in many ways: a coding flaw that lets an outsider read tables, a backup left in an open storage bucket, a stolen laptop, or an insider with too much access. If the table holds plain passwords, every account is compromised the moment it's read. Because many people reuse passwords, accounts on unrelated services fall too, which is the mechanism behind credential stuffing.

Encrypting the passwords sounds like a fix, but encryption is reversible by design. The server needs the key to check logins, so an attacker who reaches the server often reaches the key. Hashing avoids the problem because there's no key that turns the output back into the input. The differences are laid out in hashing vs. encryption vs. encoding.

What a hash function does

A cryptographic hash function takes input of any length and returns a fixed-length string. The same input always gives the same output. A tiny change to the input produces a completely different result, and there's no practical way to work backward from the output.

That's enough to check a password. At sign-up, the server hashes what you typed and saves the result. At sign-in, it hashes the new attempt and compares. The server learns whether the two match without ever needing to keep the original.

You can try the idea with the hash generator: change one character and watch the whole output change. Use made-up strings for experiments, never a real password.

How a site checks a password it does not keep
1 Sign-up You choose a password. The server creates a random salt for your account. 2 Hash and store Password plus salt go through a slow hash. Only the salt, settings and result are saved. 3 Sign-in You type the password again over an encrypted connection. 4 Recompute The server reads your salt and settings and runs the same calculation. 5 Compare Matching results mean access. The typed password is then discarded. Sign-up You choose a password. The server creates a random salt for your account. 1 Hash and store Password plus salt go through a slow hash. Only the salt, settings and result are saved. 2 Sign-in You type the password again over an encrypted connection. 3 Recompute The server reads your salt and settings and runs the same calculation. 4 Compare Matching results mean access. The typed password is then discarded. 5

Why a plain hash isn't enough

"One-way" doesn't mean "safe to leak". An attacker can't reverse a hash, but they can guess: take a candidate password, hash it, and see whether it matches. With a list of common passwords and patterns, many real accounts fall quickly.

Two weaknesses make unsalted hashing especially poor. First, identical passwords produce identical hashes, so anyone reading the table can see which users share a password, and cracking one cracks all of them. Second, attackers can compute hashes for huge numbers of candidates in advance and simply look up stolen values. Rainbow tables are a space-efficient form of this precomputation.

What a salt changes

A salt is a random value generated for each account and stored next to the hash. The server feeds both the salt and the password into the hashing function. It isn't a secret. Its job is to make every account's calculation different.

Say two users both pick the password sunflower42. With different salts, their stored hashes look nothing alike. A precomputed table is useless because it would have to be rebuilt for every possible salt. An attacker with the stolen table has to test each guess separately against each account, so the cost grows with the number of users.

A good salt is generated by a cryptographically secure random number generator, is long enough that collisions are practically impossible (16 bytes is a common choice), and is new every time the password changes. A username or email address makes a poor salt because it's predictable and repeats across sites.

Slowness is a feature

General-purpose hashes such as SHA-256 are designed to be fast, which is great for verifying files and terrible for passwords. Graphics cards and custom hardware can try enormous numbers of fast hashes per second, and a salt does nothing to slow a single guess.

Password-hashing functions are built to be expensive, with a cost you can tune. Your server pays that cost once per login. An attacker pays it for every guess.

Function What makes it costly Notes
Argon2id Memory and time, both adjustable Published as RFC 9106; a strong default for new systems
scrypt Memory and time Published as RFC 7914
bcrypt Repeated key setup, adjustable work factor Long established; many implementations only read the first 72 bytes
PBKDF2 Many iterations of another hash Defined in RFC 8018; uses little memory, so iteration counts need to be high

Memory-hard functions such as Argon2id and scrypt are attractive because memory is harder to parallelize cheaply than raw arithmetic. A chip with thousands of small cores can't give each of them a large block of memory.

Settings should be as high as your servers can tolerate at peak login volume, and revisited as hardware improves. Current recommended parameters change, so check the guidance from OWASP or the documentation of your framework before choosing numbers.

Fast hash versus password hash
Fast general-purpose hash Built to process lots of data quickly Guessing hardware can test candidates at huge rates No built-in work factor Fine for file integrity, wrong for passwords Password-hashing function Built to be expensive on purpose Each guess costs time and often memory Work factor can be raised over the years Handles a per-account salt as a standard input VS Fast general-purpose hash Built to process lots of data quickly Guessing hardware can test candidates at huge rates No built-in work factor Fine for file integrity, wrong for passwords Password-hashing function Built to be expensive on purpose Each guess costs time and often memory Work factor can be raised over the years Handles a per-account salt as a standard input

What the stored value looks like

Modern password libraries usually save everything needed for verification in one text string. That includes the algorithm, its version, the cost settings, the salt and the hash. Keeping the settings with each record is what lets a site raise the cost later without breaking existing accounts.

Reading a stored Argon2id record (example values)
argon2id 1 v=19 2 m=65536,t=3,p=4 3 c2FsdHNhbHQ... 4 9sTqk1Yp... 5 Algorithm Tells the verifier which function to run. 1 Version The Argon2 version used to create the record. 2 Cost settings Memory in kibibytes, number of passes and degree of parallelism. 3 Salt Random per account. Stored openly beside the hash. 4 Hash The result that each sign-in attempt must reproduce. 5 argon2id 1 v=19 2 m=65536,t=3,p=4 3 c2FsdHNhbHQ... 4 9sTqk1Yp... 5 Algorithm Tells the verifier which function to run. 1 Version The Argon2 version used to create the record. 2 Cost settings Memory in kibibytes, number of passes and degree of parallelism. 3 Salt Random per account. Stored openly beside the hash. 4 Hash The result that each sign-in attempt must reproduce. 5

The $ signs separate the fields. Salt and hash are shortened placeholders.

When a user signs in successfully and the record uses older settings, the application can rehash the password with current ones and replace the record. That's the one moment the server legitimately holds the plain text. Accounts that never return keep the old format, so teams sometimes wrap old hashes inside a new one or require a reset.

Peppers and other layers

A pepper is a secret value added to the calculation that's kept outside the database, for instance in a hardware security module or a separate secrets service. If only the database leaks, the attacker lacks the pepper and can't even begin guessing. If the whole application server is compromised, the pepper may go with it, so treat it as an extra layer on top of salting and slow hashing.

Peppers add operational burden. Losing one locks everyone out, and rotating one takes planning. For many small projects, getting the core algorithm right matters far more.

Other layers sit around the stored value:

  • Rate limiting slows guessing against the live login page, which hashing alone doesn't address.
  • Screening new passwords against lists of known compromised ones stops the easiest guesses from ever being set.
  • A second factor means a cracked password isn't enough by itself. See which kind of two-factor authentication is best.
  • Limited database access reduces the chance of the table leaking in the first place.

Password rules that help the hashing

NIST's digital identity guidelines (the SP 800-63 series) push sites toward rules that serve security. Allow long passwords and passphrases. Accept spaces and any printable characters. Don't force arbitrary mixes of symbols or scheduled changes, which push people toward predictable patterns. Require a change when there's evidence of compromise. Let password managers paste into the field.

Developers should rely on a maintained library or framework function and avoid assembling the scheme by hand. In PHP, for example, password_hash() and password_verify() handle salts and settings for you. Most mainstream languages have an equivalent. A well-tested library also compares results in a way that doesn't leak timing information.

Two related mistakes are easy to miss. Passwords can end up in server logs when request bodies are recorded for debugging, and a truncating algorithm can silently ignore the end of a long passphrase. Check both.

What you can tell as a user

You can't inspect a site's database, but a few behaviors are revealing.

  • A "forgot password" message that contains your current password means it was stored recoverably.
  • A support agent who can read your password back to you means the same.
  • A low maximum length or a ban on common symbols hints at old handling, though it doesn't prove plain-text storage.
  • A reset link that expires and asks you to choose a new password is the expected behavior.

You can protect yourself whatever the site does. Use a unique password for every account, ideally from a password manager. A long random password stays out of reach even when the hashing is mediocre, and a unique one keeps a bad site from endangering your other accounts. Where a service offers passkeys, there's no password hash to steal at all, because the server holds only a public key.

Common questions

Is a salt supposed to be secret?

No. It sits beside the hash in the same record. It defeats precomputation and hides which users share passwords even when the attacker can read it.

Does hashing in the browser solve the problem?

On its own, no. If the browser sends a hash and the server accepts it as proof, that hash effectively becomes the password. The server still needs to apply its own slow, salted hashing to whatever it receives, and the connection needs HTTPS.

If a breach notice says passwords were hashed, am I safe?

Safer, depending on the details. Change that password, and change it anywhere you reused it. The guide on what to do after a data breach notice covers the rest.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Cybersecurity →

Latest news on this topic