using System.Security.Cryptography;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
public static void Main()
String password = "admin123";
var hashes = Hash.CreatePasswordHash(password);
var hashPW = Convert.ToBase64String(hashes.passwordHash);
var hashSalt = Convert.ToBase64String(hashes.passwordSalt);
Console.WriteLine("Hash!...");
Console.WriteLine("password: " + password);
Console.WriteLine("passwordHash: " + hashPW);
Console.WriteLine("passwordSalt: " + hashSalt);
Console.WriteLine("Verify!...");
bool isAutenticated = Hash.VerifyPasswordHash(password, Convert.FromBase64String(hashPW), Convert.FromBase64String(hashSalt));
Console.WriteLine("OK: " + isAutenticated);
static public (byte[] passwordHash, byte[] passwordSalt) CreatePasswordHash(string password)
byte[] passwordSalt = CreateSalt();
byte[] passwordHash = KeyDerivation.Pbkdf2(
prf: KeyDerivationPrf.HMACSHA256,
numBytesRequested: 256 / 8);
return (passwordHash, passwordSalt);
static public bool VerifyPasswordHash(string password, byte[] passwordHashFromDB, byte[] passwordSaltFromDB)
byte[] passwordHash = KeyDerivation.Pbkdf2(
salt: passwordSaltFromDB,
prf: KeyDerivationPrf.HMACSHA256,
numBytesRequested: 256 / 8);
return CryptographicOperations.FixedTimeEquals(passwordHash, passwordHashFromDB);
static private byte[] CreateSalt()
byte[] salt = new byte[128 / 8];
using (var rng = RandomNumberGenerator.Create())