
ليه SHA-256 مينفعش للـ passwords؟ الـ slow hashing بـ Argon2id و bcrypt
- كاتب موثّق
محتويات المقال
الفكرة الغلط الشائعة
اللي بيحصل فعلًا
المهاجم مش بيعكس الـ hash، بيخمّن
الـ salt بيمنع الاختصارات، مش التخمين
الـ password hashing محتاج 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 eachالـ memory كمان بتخلّي التخمين غالي
من SHA-256 لـ 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 بتاع ASP.NET Core Identity. الـ source بتاعه بيوصف الـ format إنه "PBKDF2 with HMAC-SHA512, 128-bit salt, 256-bit subkey"، وMicrosoft موثّقة عدد الـ iterations: "Default is 100,000." ورقم OWASP للـ PBKDF2-HMAC-SHA512 هو 220,000، فالكود بيزوّده. والـ algorithm والعدد بيتحفظوا جوه الـ hash نفسه، ولو hash محفوظ عدد الـ iterations بتاعه أقل من اللي متظبط، الـ verification بترجّع SuccessRehashNeeded، فالمستخدمين بينتقلوا للـ cost الجديد واحد واحد مع كل login. أما Argon2id، فالـ base libraries بتاعة .NET 10 مفيهاش implementation ليه، فاستخدم library بتتحدّث باستمرار. والتجربة بتاعتنا استخدمت NSec 26.4، وهي wrapper لـ libsodium (من الـ community، مش رسمي).
تطبقها إزاي في مشروعك
اعرف الـ passwords بتتخزّن إزاي دلوقتي دوّر على SHA256وSHA1وMD5وHashDataجنب أي حاجة اسمها password، وما تنساش الـ migration scripts القديمة وأدوات الـ admin.اختار الـ scheme Argon2id من library بتتحدّث للسيستمات الجديدة. وPBKDF2 بـ HMAC-SHA256 و600,000 iteration لو FIPS-140 مطلوب. ومع ASP.NET Core Identity، خلّي IterationCountعلى الأقل 220,000.اعمل benchmark على السيرفرات بتاعتك القاعدة التقريبية عند OWASP إن الـ hash "should take less than one second". قيس تحت ضغط login واقعي، واختار أعلى cost لسه مريح. احفظ الـ parameters وانقل مع الـ login خلّي الـ algorithm والـ cost مع كل hash. اعمل rehash مع الـ login الناجح، ولف أو اعمل expire للـ rows القديمة اللي بـ SHA-256. زوّد الـ cost مع الوقت NIST بتقول إن الـ cost factor "SHOULD be increased over time". حط تذكير كل سنة في الـ calendar إنك تعيد الـ benchmark.
الخلاصة
كسر الـ passwords معناه تخمين، فسرعة الـ hash الواحد هي اللي بتحمي المستخدمين بتوعك. الـ salt بيخلّي المهاجم يشتغل على كل user لوحده، لكنه مش بيبطّأ التخمينة الواحدة. استخدم password hashing scheme فيها cost factor: Argon2id الأول، وPBKDF2 لو FIPS مطلوب. الـ ASP.NET Core Identity بيستخدم PBKDF2-HMAC-SHA512 بـ 100,000 iteration كـ default، زوّدها لـ 220,000. احفظ الـ parameters مع كل hash، واعمل rehash مع الـ login، وزوّد الـ cost كل ما الهاردوير يسرع.
📚 المصادر
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)