42 lines
895 B
C#
42 lines
895 B
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
using TextSender_API.Models;
|
|
|
|
namespace TextSender_API.Controllers;
|
|
|
|
public class PasswordVerification
|
|
{
|
|
#region Fiends
|
|
private int _keySize = 64;
|
|
private int _iterations = 350000;
|
|
#endregion
|
|
|
|
#region Constructors
|
|
#endregion
|
|
|
|
#region Methods
|
|
public Salt CreateSalt(User user)
|
|
{
|
|
var salt = new Salt();
|
|
salt.Key = RandomNumberGenerator.GetBytes(this._keySize);
|
|
|
|
return salt;
|
|
}
|
|
|
|
public string HashPassword(User user, Salt salt)
|
|
{
|
|
var hashAlgorithm = HashAlgorithmName.SHA512;
|
|
|
|
var hash = Rfc2898DeriveBytes.Pbkdf2(
|
|
Encoding.UTF8.GetBytes(user.Password!),
|
|
salt.Key!,
|
|
this._iterations,
|
|
hashAlgorithm,
|
|
this._keySize);
|
|
|
|
return Convert.ToHexString(hash);
|
|
}
|
|
#endregion
|
|
} |