35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System.Security.Cryptography;
|
|
|
|
namespace KronaStaffTerminal.Services;
|
|
|
|
public static class AdminPinService
|
|
{
|
|
private const int Iterations = 120_000;
|
|
|
|
public static (string Salt, string Hash) Hash(string pin)
|
|
{
|
|
var salt = RandomNumberGenerator.GetBytes(16);
|
|
var hash = Rfc2898DeriveBytes.Pbkdf2(pin, salt, Iterations, HashAlgorithmName.SHA256, 32);
|
|
return (Convert.ToBase64String(salt), Convert.ToBase64String(hash));
|
|
}
|
|
|
|
public static bool Verify(string pin, string saltBase64, string hashBase64)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(pin) || string.IsNullOrWhiteSpace(saltBase64) || string.IsNullOrWhiteSpace(hashBase64))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var salt = Convert.FromBase64String(saltBase64);
|
|
var expected = Convert.FromBase64String(hashBase64);
|
|
var actual = Rfc2898DeriveBytes.Pbkdf2(pin, salt, Iterations, HashAlgorithmName.SHA256, expected.Length);
|
|
return CryptographicOperations.FixedTimeEquals(actual, expected);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|