forked from mtusi2026/mtusi2026lab2
29 lines
765 B
C#
29 lines
765 B
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Isopoh.Cryptography.Argon2;
|
|
|
|
namespace Messenger.Api.Services;
|
|
|
|
public sealed class PasswordService
|
|
{
|
|
public string Hash(string password)
|
|
{
|
|
var salt = RandomNumberGenerator.GetBytes(16);
|
|
var config = new Argon2Config
|
|
{
|
|
Type = Argon2Type.HybridAddressing,
|
|
Version = Argon2Version.Nineteen,
|
|
TimeCost = 3,
|
|
MemoryCost = 32768,
|
|
Lanes = 1,
|
|
Threads = 1,
|
|
Password = Encoding.UTF8.GetBytes(password),
|
|
Salt = salt,
|
|
HashLength = 32
|
|
};
|
|
return Argon2.Hash(config);
|
|
}
|
|
|
|
public bool Verify(string hash, string password) => Argon2.Verify(hash, password);
|
|
}
|