Path: redb/redb.Core/Security/SimplePasswordHasher.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/SimplePasswordHasher.cs ---- using System; using System.Security.Cryptography; using System.Text; namespace redb.Core.Security; /// /// Password hasher implementation using SHA256 + salt. /// Implements IPasswordHasher interface for DI. /// Consider using for better brute-force resistance. /// public class SimplePasswordHasher : IPasswordHasher { /// /// Hash password with salt. /// /// Plain-text password /// Hashed password with salt in format: base64(salt):base64(hash) public string HashPassword(string password) { if (string.IsNullOrEmpty(password)) throw new ArgumentException("Password cannot be empty", nameof(password)); var salt = GenerateSalt(); var hash = ComputeHash(password, salt); return $"{Convert.ToBase64String(salt)}:{Convert.ToBase64String(hash)}"; } /// /// Verify password against stored hash. /// /// Plain-text password to verify /// Stored hashed password from DB /// True if password matches public bool VerifyPassword(string password, string hashedPassword) { if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hashedPassword)) return false; try { var parts = hashedPassword.Split(':'); if (parts.Length != 2) return false; var salt = Convert.FromBase64String(parts[0]); var storedHash = Convert.FromBase64String(parts[1]); var computedHash = ComputeHash(password, salt); return CryptographicOperations.FixedTimeEquals(storedHash, computedHash); } catch { return false; } } /// /// Generate random salt (256 bits). /// private static byte[] GenerateSalt() { var salt = new byte[32]; using var rng = RandomNumberGenerator.Create(); rng.GetBytes(salt); return salt; } /// /// Compute SHA256 hash of password with salt. /// private static byte[] ComputeHash(string password, byte[] salt) { 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); return SHA256.HashData(saltedPassword); } }