Path: redb/redb.Core/Security/BcryptPasswordHasher.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Security/BcryptPasswordHasher.cs ---- using System; using System.Security.Cryptography; using System.Text; namespace redb.Core.Security; /// /// Password hasher using BCrypt (recommended for production). /// Backward-compatible: can verify legacy SHA256+salt hashes from . /// New passwords are always hashed with BCrypt. /// public class BcryptPasswordHasher : IPasswordHasher { private readonly int _workFactor; /// /// Creates a BCrypt password hasher. /// /// BCrypt work factor (default: 12, ~250ms per hash). public BcryptPasswordHasher(int workFactor = 12) { if (workFactor < 4 || workFactor > 31) throw new ArgumentOutOfRangeException(nameof(workFactor), workFactor, "Work factor must be between 4 and 31."); _workFactor = workFactor; } /// public string HashPassword(string password) { if (string.IsNullOrEmpty(password)) throw new ArgumentException("Password cannot be empty", nameof(password)); return BCrypt.Net.BCrypt.HashPassword(password, _workFactor); } /// public bool VerifyPassword(string password, string hashedPassword) { if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hashedPassword)) return false; try { // BCrypt hashes start with "$2a$", "$2b$", or "$2y$" if (hashedPassword.StartsWith("$2", StringComparison.Ordinal)) { return BCrypt.Net.BCrypt.Verify(password, hashedPassword); } // Legacy SHA256+salt format: "base64(salt):base64(hash)" if (hashedPassword.Contains(':')) { return VerifyLegacySha256(password, hashedPassword); } return false; } catch { return false; } } /// /// Checks whether the stored hash is in legacy SHA256 format and needs rehashing to BCrypt. /// Use this in login flow: if true, rehash with after successful verify. /// public static bool NeedsRehash(string hashedPassword) { if (string.IsNullOrEmpty(hashedPassword)) return true; // Legacy format (SHA256+salt) or unknown → needs rehash return !hashedPassword.StartsWith("$2", StringComparison.Ordinal); } /// /// Verify password against legacy SHA256+salt format from . /// private static bool VerifyLegacySha256(string password, string hashedPassword) { var parts = hashedPassword.Split(':'); if (parts.Length != 2) return false; var salt = Convert.FromBase64String(parts[0]); var storedHash = Convert.FromBase64String(parts[1]); var passwordBytes = Encoding.UTF8.GetBytes(password); var saltedPassword = new byte[passwordBytes.Length + salt.Length]; Array.Copy(passwordBytes, 0, saltedPassword, 0, passwordBytes.Length); Array.Copy(salt, 0, saltedPassword, passwordBytes.Length, salt.Length); var computedHash = SHA256.HashData(saltedPassword); return CryptographicOperations.FixedTimeEquals(storedHash, computedHash); } }