
SHA-256 Is the Wrong Tool for Passwords: Slow Hashing with Argon2id and bcrypt
- كاتب موثّق
محتويات المقال
The common misconception
What actually happens
Attackers don't reverse hashes, they guess
A salt stops shortcuts, not guessing
Password hashing needs a cost factor
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 eachMemory makes guessing expensive too
From SHA-256 to a password hasher
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);}using Microsoft.AspNetCore.Identity;using Microsoft.Extensions.Options;public sealed class Passwords{ // PBKDF2-HMAC-SHA512 with a random salt; the default is 100,000 private readonly PasswordHasher<User> _hasher = new(Options.Create( new PasswordHasherOptions { IterationCount = 220_000 })); public string Hash(User user, string password) => _hasher.HashPassword(user, password); public bool Verify(User user, string password) { var result = _hasher.VerifyHashedPassword(user, user.PasswordHash, password); if (result == PasswordVerificationResult.SuccessRehashNeeded) user.PasswordHash = Hash(user, password); // raise the cost on login return result != PasswordVerificationResult.Failed; }}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).
How to apply it in your project
Find how passwords are stored today Search for SHA256,SHA1,MD5andHashDatanear anything called password, including old migration scripts and admin tools.Pick the scheme Argon2id 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 IterationCountto at least 220,000.Benchmark on your own servers OWASP'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. Store parameters and migrate on login Keep the algorithm and cost with each hash. Rehash on successful login, and wrap or expire the old SHA-256 rows. Raise the cost over time NIST 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.
📚 المصادر
Password Storage Cheat Sheet — OWASP Cheat Sheet Series NIST SP 800-63B-4: Authentication and Authenticator Management RFC 9106: Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications PasswordHasherOptions.IterationCount Property — Microsoft Learn PasswordHasher.cs (ASP.NET Core 10) — dotnet/aspnetcore on GitHub
التعليقات (0)