Egy kis authentikáció

This commit is contained in:
2025-01-21 15:52:30 +01:00
parent 3b11c6adc5
commit 30d82fc9bf
12 changed files with 186 additions and 12 deletions
+2
View File
@@ -6,5 +6,7 @@
public string Email { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string UserName { get;set; } = null!;
public string Password { get; set; }
}
}
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace WorkFlowCheck.Common.Security
{
public static class PasswordHasher
{
// Jelszó hashelése
public static string HashPassword(string password)
{
// Generate a random salt
byte[] salt = new byte[16];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
// Hash the password with the salt
using (var rfc2898 = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA256))
{
byte[] hash = rfc2898.GetBytes(32); // 256-bit hash
byte[] hashBytes = new byte[48]; // Salt (16 bytes) + Hash (32 bytes)
Array.Copy(salt, 0, hashBytes, 0, 16);
Array.Copy(hash, 0, hashBytes, 16, 32);
return Convert.ToBase64String(hashBytes);
}
}
// Jelszó ellenőrzése
public static bool VerifyPassword(string storedPasswordHash, string inputPassword)
{
byte[] hashBytes = Convert.FromBase64String(storedPasswordHash);
// Extract salt (first 16 bytes) and stored hash (next 32 bytes)
byte[] salt = new byte[16];
byte[] storedHash = new byte[32];
Array.Copy(hashBytes, 0, salt, 0, 16);
Array.Copy(hashBytes, 16, storedHash, 0, 32);
// Hash the input password with the same salt
using (var rfc2898 = new Rfc2898DeriveBytes(inputPassword, salt, 10000, HashAlgorithmName.SHA256))
{
byte[] inputHash = rfc2898.GetBytes(32);
// Compare the stored hash and the calculated hash
return CryptographicOperations.FixedTimeEquals(storedHash, inputHash);
}
}
}
}