Two jobs that look alike
SHA-256, bcrypt and Argon2 all turn input into a fixed-length value that cannot be turned back. That is where the similarity ends. They are built for opposite goals, and using one for the other's job is one of the most common security mistakes in web applications.
| General-purpose hash | Password hash | |
|---|---|---|
| Examples | SHA-256, SHA-512, SHA-3, BLAKE2 | Argon2id, scrypt, bcrypt, PBKDF2 |
| Designed to be | As fast as possible | As slow as you can afford |
| Same input gives | Always the same output | A different output each time (random salt) |
| Used for | Checksums, signatures, HMAC, deduplication | Storing passwords and deriving keys from them |
A fast hash is right when the input is large or already unguessable and you need to process a lot of it. A password hash is right when the input is something a person chose, because a person-chosen value can be guessed, and the only defence left is making each guess expensive.
Why a fast hash fails for passwords
Here is the password password through two fast hashes:
MD5 5f4dcc3b5aa765d61d8327deb882cf99
SHA-256 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8Every system that stores unsalted SHA-256 stores exactly that value for every user whose password is password. Three things follow from that.
- Lookup tables work. Hashes of common passwords are computed once and published, so cracking a weak password is a search, not a computation.
- Duplicates show. Two rows with the same hash have the same password, and cracking one cracks all of them.
- Guessing is cheap. SHA-256 is designed to run fast on hardware, and GPUs compute it in parallel, so an attacker with a stolen database can try billions of candidates per second against it.
MD5 and SHA-1 have a further problem - practical collisions, two different inputs with the same hash, were published for MD5 in 2004 and for SHA-1 in 2017 - but that is not what breaks password storage. SHA-256 has no known collisions and is just as unsuitable, because the attack on stored passwords is guessing, and SHA-256 makes guessing fast.
What a salt does, and what it does not
A salt is a random value generated per user and stored next to the hash. Hashing the salt together with the password makes equal passwords produce different hashes:
SHA-256("k9Qx2fLp" + "password")
1239c2a40d08da80f4c57839520c4933d87a3b51ddc13cbcdaa354c0cc93f220
SHA-256("Zr41mWvb" + "password")
4d3f33b90396d4dca81e8c88e643671f048e4e2c1db48951d46b9ed0739e96c4That defeats lookup tables and hides duplicates. It does nothing about the speed of guessing: an attacker who wants one particular account takes that row's salt and guesses as fast as before. Salting is necessary, not sufficient. Every password hash listed below generates and stores the salt for you, which is one more reason not to build this out of SHA-256 by hand.
A pepper is a second secret, the same for all users and kept outside the database (in a secrets manager or a hardware security module). It helps only when the database leaks and the pepper does not. It is an optional extra, never a replacement for a slow hash.
What a work factor buys
A password hash has a cost setting that controls how much work one hash takes. Your server pays that cost once per login. An attacker pays it once per guess, and makes billions of guesses. Raising the cost so a login check takes a fraction of a second is barely noticeable to a user and cuts an attacker's guessing rate by the same factor.
Argon2 and scrypt are also memory-hard: each hash needs a block of memory, not only processor time. GPUs and custom hardware get their speed from thousands of cores working in parallel on very little memory each, so requiring tens of megabytes per guess takes most of that advantage away. bcrypt uses a small, fixed amount of memory and PBKDF2 almost none, which is why they are now the second choice.
Long random passwords, such as those from a password generator, are out of reach of guessing whatever the hash. But a server cannot tell a random password from Summer2026!, so it has to store every password as if it were the weak one.
Which algorithm and settings to use
The OWASP Password Storage Cheat Sheet is the usual reference. Its current minimums, in order of preference:
| Algorithm | Minimum settings | Notes |
|---|---|---|
| Argon2id | 19 MiB of memory, 2 iterations, parallelism 1 | First choice. Winner of the 2015 Password Hashing Competition. |
| scrypt | N = 2^17, r = 8, p = 1 | Memory-hard. Built into Node.js as crypto.scrypt. |
| bcrypt | Work factor 10 or more | Reads at most 72 bytes of the password; longer input is cut off. |
| PBKDF2 | 600,000 iterations with HMAC-SHA-256 | Only where FIPS-140 validated algorithms are required. |
These are floors. Measure on your own servers and raise the cost until one hash takes as long as you are willing to spend on a login, keeping in mind that a login endpoint under attack runs it many times at once.
All of these produce a self-describing string that stores the algorithm, the settings and the salt alongside the hash, so the stored value is all you need to verify later. The two common shapes look like this:
$argon2id$v=19$m=19456,t=2,p=1$<salt, Base64>$<hash, Base64>
$2b$12$<22-character salt><31-character hash>Because the settings travel with each hash, you can raise them later: when a user logs in successfully, check whether their stored hash uses the current settings, and if not, rehash the password they just typed and save the new value.
- Use your framework's built-in password hashing where it has one. PHP's
password_hash(), Django's password hashers and ASP.NET Core Identity'sPasswordHasherall handle salts, formats and upgrades. - Otherwise use a maintained library for Argon2id, such as
argon2for Node.js orargon2-cffifor Python. - Never compare hashes with
==; use the library's verify function, which compares in constant time. - Do not invent a scheme, such as SHA-256 applied 10,000 times. PBKDF2 is the standardised version of that idea, and it is the weakest of the four.
Where SHA-256 is the right tool
Everywhere the input is not a guessable secret, a fast hash is exactly what you want. A slow one would only waste time.
- Checksums: confirming a download or a backup has not changed. The hash generator computes SHA-256, SHA-512, SHA-1 and MD5 for text, so you can compare against a published digest.
- Message authentication with HMAC: proving a webhook or API request came from someone who knows a shared secret.
- Signed tokens: a JWT with
alg: HS256is signed with HMAC-SHA-256, which you can inspect in the JWT decoder. - Storing API keys, session tokens and password-reset tokens that your server generated with enough randomness to be unguessable. There is nothing to brute-force, so SHA-256 of the token is sufficient.
An HMAC mixes a secret key into the hash, so only someone with the key can produce the same value. For example, a webhook sender signing a body with the secret my-webhook-secret:
{"event":"order.paid","id":1042}7cffec8ce854db0838cb92cd452687e0b8db72bf1b887ec81e153030047edd13The receiver computes the same HMAC over the raw body it received and compares the two in constant time. Any change to the body or a different key gives a completely different value.
