25 lines
881 B
C#
25 lines
881 B
C#
using System.Security.Cryptography;
|
|
|
|
namespace Server.Services.Auth;
|
|
|
|
public sealed class Pbkdf2PasswordHasher : IPasswordHasher
|
|
{
|
|
private const int SaltSize = 32;
|
|
private const int HashSize = 64;
|
|
private const int Iterations = 150_000; // good default; tune for your hardware
|
|
|
|
public void Hash(string password, out byte[] hash, out byte[] salt)
|
|
{
|
|
salt = RandomNumberGenerator.GetBytes(SaltSize);
|
|
using var pbkdf2 = new Rfc2898DeriveBytes(password, salt, Iterations, HashAlgorithmName.SHA256);
|
|
hash = pbkdf2.GetBytes(HashSize);
|
|
}
|
|
|
|
public bool Verify(string password, byte[] hash, byte[] salt)
|
|
{
|
|
using var pbkdf2 = new Rfc2898DeriveBytes(password, salt, Iterations, HashAlgorithmName.SHA256);
|
|
var test = pbkdf2.GetBytes(HashSize);
|
|
return CryptographicOperations.FixedTimeEquals(test, hash);
|
|
}
|
|
}
|