Adds User Component

This commit is contained in:
Janus Knudsen 2025-03-04 17:13:02 +01:00
parent 73a1f11e99
commit 69758735de
14 changed files with 222 additions and 65 deletions

View file

@ -1,47 +0,0 @@
namespace PlanTempus.Core.Entities.Users
{
public static class PasswordHasher
{
private const int _saltSize = 16; // 128 bit
private const int _keySize = 32; // 256 bit
private const int _iterations = 100000;
public static string HashPassword(string password)
{
using (var algorithm = new System.Security.Cryptography.Rfc2898DeriveBytes(
password,
_saltSize,
_iterations,
System.Security.Cryptography.HashAlgorithmName.SHA256))
{
var key = Convert.ToBase64String(algorithm.GetBytes(_keySize));
var salt = Convert.ToBase64String(algorithm.Salt);
return $"{_iterations}.{salt}.{key}";
}
}
public static bool VerifyPassword(string hash, string password)
{
var parts = hash.Split('.', 3);
if (parts.Length != 3)
{
return false;
}
var iterations = Convert.ToInt32(parts[0]);
var salt = Convert.FromBase64String(parts[1]);
var key = Convert.FromBase64String(parts[2]);
using (var algorithm = new System.Security.Cryptography.Rfc2898DeriveBytes(
password,
salt,
iterations,
System.Security.Cryptography.HashAlgorithmName.SHA256))
{
var keyToCheck = algorithm.GetBytes(_keySize);
return keyToCheck.SequenceEqual(key);
}
}
}
}