SharpRSS/SharpRSS.API/Cryptography/Hasher.cs
2023-09-17 21:41:31 +02:00

33 lines
1.1 KiB
C#

using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace SharpRSS.API.Cryptography
{
public static class Hasher
{
private const int KeySize = 128;
private const int Iterations = 420069;
private static readonly HashAlgorithmName Algorithm = HashAlgorithmName.SHA512;
public static byte[] HashPassword(string password, out byte[] salt)
{
salt = RandomNumberGenerator.GetBytes(KeySize);
return HashInternal(password, salt);
}
public static bool ComparePasswords(string password, byte[] hash, byte[] salt)
{
byte[] passwordHashed = HashInternal(password, salt);
if (hash.Length != passwordHashed.Length)
return false;
return !hash.Where((t, i) => t != passwordHashed[i]).Any();
}
private static byte[] HashInternal(string password, byte[] salt)
{
var hash = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, Iterations, Algorithm,KeySize);
return hash;
}
}
}