الانتقال إلى المحتوى
الأمان (Security)Secure Coding· 7 دقائق للقراءة

SHA-256 Is the Wrong Tool for Passwords: Slow Hashing with Argon2id and bcrypt

A salt stops shortcuts, not guessing. Password hashing needs a cost factor that makes every guess expensive, and a way to raise it later.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A staging database backup ends up on a public file share. The incident note tries to calm everyone down: "Passwords are hashed with SHA-256 and a unique salt per user." The security lead forces a password reset for every account anyway. The note was accurate about the hashing. It was wrong about what that hashing buys, because nobody needs to reverse a hash to recover the password behind it.

The common misconception

The belief: SHA-256 is a strong, standard cryptographic hash. Add a unique random salt so precomputed tables don't work, and the stored passwords are safe even if the database leaks.

Most of that is true, which is why the belief is so common. SHA-256 isn't broken: nobody can compute a password back from its hash. A unique salt really does defeat precomputed rainbow tables, and it hides which users share a password. Every mainstream language ships SHA-256 in its standard library, and code reviewers recognize it. The missing piece is speed. A secure general-purpose hash is designed to be fast, and fast is the one property you don't want here.

What actually happens

Attackers don't reverse hashes, they guess

OWASP's Password Storage Cheat Sheet describes the attack plainly: pick a password you think the victim chose, calculate its hash, compare it to the stored one, and repeat with lists of leaked passwords, dictionaries or brute force. The stays unbroken the whole time. The only thing that matters is how fast each guess is. That's why OWASP says "Fast hashing algorithms such as SHA-256 are not suitable for password storage because they allow attackers to perform large numbers of guesses quickly." NIST's SP 800-63B-4 notes "the current ability of attackers to compute many billions of hashes per second in an offline environment that is not subject to rate limiting."

A salt stops shortcuts, not guessing

A forces the attacker to work on each user separately. In OWASP's words, the attacker "has to crack hashes one at a time using the respective salt rather than calculating a hash once and comparing it against every stored hash." That's valuable, but it doesn't make a single guess slower. Against one account, salted SHA-256 is still one fast hash per guess.

Password hashing needs a cost factor

NIST requires a and explains what that means: "Password hashing schemes take a password, a salt, and a cost factor as inputs and generate a password hash. Their purpose is to make each password guess more expensive for an attacker who has obtained a hashed password file". We timed each option on one core of an Intel Core i7-13650HX laptop, with .NET 10.0.11, taking the median of 7 runs:

text
Scheme                              Time per guess  Guesses per secondSHA-256 + salt                      ~0.22 µs        ~4.6 millionPBKDF2-HMAC-SHA512, 100,000         ~57 ms          ~17PBKDF2-HMAC-SHA256, 600,000         ~81 ms          ~12PBKDF2-HMAC-SHA512, 220,000         ~131 ms         ~8Argon2id, 19 MiB, t=2, p=1          ~17 ms          ~60, 19 MiB each

PBKDF2 at OWASP's 600,000 iterations costs about 370,000 times more per guess than salted SHA-256 on the same machine. For a user logging in, 81 ms is invisible. For someone testing a wordlist against a dump, it's the whole difference.

Memory makes guessing expensive too

PBKDF2 only burns CPU time, and that kind of work runs well on GPUs. RFC 9106 describes Argon2 as "a memory-hard function", and it names Argon2id the primary variant: "Argon2id MUST be supported by any implementation of this document". In our lab, Argon2id was faster than PBKDF2 per call, but every single guess needed its own 19 MiB, which limits how many guesses an attacker can run side by side. That's why OWASP ranks it first, with scrypt second and bcrypt only "in legacy systems where Argon2 and scrypt are not available."

From SHA-256 to a password hasher

C# Passwords.cs
using System.Security.Cryptography;using System.Text;public static class Passwords{    // One fast hash per guess, salt or not    public static byte[] Hash(byte[] salt, string password) =>        SHA256.HashData([.. salt, .. Encoding.UTF8.GetBytes(password)]);    public static bool Verify(byte[] salt, byte[] stored, string password) =>        CryptographicOperations.FixedTimeEquals(Hash(salt, password), stored);}

The wrong version even compares in constant time, and it's still wrong, because each guess costs one SHA-256. The right version uses ASP.NET Core Identity's PasswordHasher. Its source describes the format as "PBKDF2 with HMAC-SHA512, 128-bit salt, 256-bit subkey", and Microsoft documents the iteration count: "Default is 100,000." OWASP's figure for PBKDF2-HMAC-SHA512 is 220,000, so the code raises it. The algorithm and count are stored inside the hash, and when a stored hash uses fewer iterations than configured, verification returns SuccessRehashNeeded, so users move to the new cost as they log in. For Argon2id, .NET 10's base libraries have no implementation; use a maintained library. Our lab used NSec 26.4, a libsodium wrapper (community, not official).

AlgorithmOWASP minimumWhen to use it
Argon2id19 MiB memory, t=2, p=1First choice for new systems
scryptN=2^17, r=8, p=1When Argon2id isn't available
bcryptWork factor 10 or moreLegacy systems; 72-byte input limit
PBKDF2-HMAC-SHA256600,000 iterationsWhen FIPS-140 compliance is required
PBKDF2-HMAC-SHA512220,000 iterationsASP.NET Core Identity's scheme, after raising its default
SHA-256 + saltNot suitableNever for passwords

How to apply it in your project

  1. Find how passwords are stored todaySearch for SHA256, SHA1, MD5 and HashData near anything called password, including old migration scripts and admin tools.
  2. Pick the schemeArgon2id from a maintained library for new systems. PBKDF2 with HMAC-SHA256 at 600,000 iterations when FIPS-140 applies. With ASP.NET Core Identity, set IterationCount to at least 220,000.
  3. Benchmark on your own serversOWASP's rule of thumb is that a hash "should take less than one second". Measure under realistic login load and pick the highest cost that stays comfortable.
  4. Store parameters and migrate on loginKeep the algorithm and cost with each hash. Rehash on successful login, and wrap or expire the old SHA-256 rows.
  5. Raise the cost over timeNIST says the cost factor "SHOULD be increased over time". Put a yearly reminder on the calendar to rerun the benchmark.

Key takeaways

  • Password cracking is guessing, so the speed of one hash is what protects your users.
  • A salt makes attackers work per user, but it doesn't make a single guess slower.
  • Use a password hashing scheme with a cost factor: Argon2id first, PBKDF2 when FIPS is required.
  • ASP.NET Core Identity uses PBKDF2-HMAC-SHA512 with 100,000 iterations by default; raise it to 220,000.
  • Store the parameters with each hash, rehash on login, and raise the cost as hardware gets faster.

📚 المصادر

  1. Password Storage Cheat Sheet — OWASP Cheat Sheet Series
  2. NIST SP 800-63B-4: Authentication and Authenticator Management
  3. RFC 9106: Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications
  4. PasswordHasherOptions.IterationCount Property — Microsoft Learn
  5. PasswordHasher.cs (ASP.NET Core 10) — dotnet/aspnetcore on GitHub

التعليقات (0)