Files
systemrevivalserver/Services/Auth/Pbkdf2PasswordHasher.cs
T

25 lines
878 B
C#

using System.Security.Cryptography;
namespace GameServices.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);
}
}