Blog ·
Why You Should Not Use SHA-256 Alone to Hash Passwords
SHA-256 is built for speed, which is the wrong property for passwords. Use a slow, salted password hash such as bcrypt, scrypt or Argon2 instead. Here's why.
Hash Generator
Generate SHA-256, SHA-384, SHA-512 and SHA-1 hashes.
Don't store passwords as plain SHA-256 digests. SHA-256 is designed to be fast, and that speed is exactly what makes it a poor choice for passwords. Use a password hashing function such as bcrypt, scrypt or Argon2, which are built to be slow, salted and tunable.
You can look at SHA-256 output yourself with the Hash Generator, which produces SHA-256, SHA-384, SHA-512 and SHA-1 digests of text. It's useful for checksums and for seeing what a digest looks like. It's the wrong tool for choosing how to store user passwords, and the rest of this post explains why.
What SHA-256 is designed for
SHA-256 is a cryptographic hash function from the SHA-2 family. It takes any input and produces a fixed 256-bit (32-byte) output. Good hash functions are deterministic, hard to invert, and resistant to finding two inputs with the same output.
It is designed to be fast. Hardware that is built for this kind of work, especially GPUs, can compute SHA-256 at rates in the billions per second. Even a general-purpose CPU computes it quickly. That speed is a feature when you're verifying a file download or signing data, and it's a liability when the input is a low-entropy secret like a password.
Why speed is the problem for passwords
If an attacker steals a database of SHA-256 password hashes, they don't need to break the hash. They guess. For each guess, they compute the hash and compare it to the stored value. The faster each guess, the more guesses they can test in a given time.
Most passwords are short, predictable, or reused from breached lists. Dictionary words, common patterns like Summer2024!, and passwords from previous leaks are all cheap to test. With fast hashes and GPUs, an attacker can run through large candidate lists quickly. Speed works in the attacker's favor.
Password hashing functions change this math. They are intentionally expensive, so each guess costs real time and, in the case of scrypt and Argon2, real memory. You pick the cost so that a legitimate login still feels fast, while an attacker testing millions of guesses faces a much bigger bill.
Salt is required, not optional
A salt is a random value unique to each password. It's stored alongside the hash and mixed in before hashing. Without a salt, two users with the same password get the same stored hash, and an attacker can precompute hashes for common passwords once and match them across the whole database. Those precomputed tables are called rainbow tables.
Plain SHA-256 has no salt built in. You have to add one yourself, and it's easy to get wrong. Concatenating a fixed application-wide string onto the password is not a per-user salt. You need a per-user, random salt. Password hashing libraries generate and store the salt for you, which is one more reason to use them.
What the password hashing functions do
These are the three you'll see most often. Each has tunable parameters that set how much work a single hash costs.
- bcrypt: Based on the Blowfish cipher. Has a cost factor where each increment doubles the work. Widely supported. It only uses the first 72 bytes of input, so very long passwords need care.
- scrypt: A memory-hard function. Its parameters (CPU/memory cost, block size, parallelization) make it expensive to run on specialized hardware that doesn't have much memory per core.
- Argon2: Won the Password Hashing Competition in 2015. Argon2id is the variant recommended for password hashing in RFC 9106. Parameters control memory, iterations and parallelism.
The parameter values you should choose change over time as hardware improves. Use the current recommendations from your library or the OWASP Password Storage Cheat Sheet rather than copying numbers from an old tutorial.
Examples: hashing a password correctly
Here is bcrypt in Node.js using the bcrypt package. The library generates the salt and stores it in the output string, so you only save one value.
const bcrypt = require('bcrypt');
const saltRounds = 12;
async function hashPassword(plain) {
return bcrypt.hash(plain, saltRounds);
}
async function checkPassword(plain, stored) {
return bcrypt.compare(plain, stored);
}The same idea in Python with the bcrypt package:
import bcrypt
def hash_password(plain: str) -> bytes:
return bcrypt.hashpw(plain.encode('utf-8'), bcrypt.gensalt(rounds=12))
def check_password(plain: str, stored: bytes) -> bool:
return bcrypt.checkpw(plain.encode('utf-8'), stored)Notice what is missing: no manual salt, no custom loop, no homemade key stretching. Use the library's verify function to compare a login attempt against the stored hash rather than comparing hash strings yourself.
Common mistakes with SHA-256 for passwords
- Hashing once with SHA-256 and storing the result. This is fast, and without a salt it's easy to attack in bulk.
- Adding a global salt or pepper and assuming that's enough. A secret pepper can be an extra layer, but it does not replace per-user salts and a slow hash.
- Looping SHA-256 thousands of times and calling it key stretching. The idea is similar in spirit, but it's not a vetted construction. Use PBKDF2, scrypt, bcrypt or Argon2 instead.
- Picking a hash from a general-purpose library without checking whether it's a password function. Many libraries offer MD5, SHA-1 and SHA-256 but not the slow variants.
- Hashing the password on the client and treating that digest as the credential. The server still needs to protect whatever gets stored, so the stored value is what matters.
When SHA-256 is the right choice
SHA-256 is fine for many jobs: checking the integrity of a file, computing content addresses, generating ETags, or building HMAC-based message authentication when combined with a secret key. The problem is when the input is a human-chosen password that needs to resist guessing.
Be careful with sensitive input in any online tool. Don't paste real production passwords into a website to test hashes. Use test values when you experiment, and do the real hashing in your own code with a password hashing library.
If you're also working with the older algorithm, our post on SHA-256 vs SHA-1 covers why SHA-1 is no longer considered secure for collision resistance.
Tokens are a different problem
Don't confuse password storage with other secrets. Session tokens, API keys and one-time codes have different threat models. If you decode a JWT to check its claims, the JWT Decoder only reads the header and payload without verifying the signature. Verification is a separate step, covered in Decode JWT vs Verify.
FAQ
Is SHA-256 with a salt good enough for passwords?
Usually not. A salt stops precomputed attacks, but SHA-256 is still fast, so an attacker with a stolen database can test a very large number of guesses. Use bcrypt, scrypt or Argon2 with tuned parameters.
Which should I choose: bcrypt, scrypt or Argon2?
Argon2id is the modern recommendation, and it's the one current guidance points to most often. bcrypt is widely deployed and well understood, so it's a reasonable choice if your platform supports it well. scrypt is also a sound option. Pick the one your language's maintained library supports well.
Do I need to rehash existing SHA-256 passwords?
If you have stored unsalted SHA-256 hashes, plan a migration. A common approach is to verify the old hash at login, and if it matches, store a new Argon2 or bcrypt hash and discard the old one. Users who never log in will keep the weaker hash, so consider forcing a password reset for dormant accounts.
Is a SHA-256 hash of a password reversible?
Not directly, but it doesn't need to be. Attackers test candidate passwords and compare hashes. For weak or common passwords, this takes very little time, which is why a slow password hashing function is needed.
Hash Generator
Generate SHA-256, SHA-384, SHA-512 and SHA-1 hashes.