47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Tesses.Backup.Models
|
|
{
|
|
public class Account
|
|
{
|
|
public bool IsAdmin {get;set;}
|
|
public long Id {get;set;}
|
|
|
|
public string Username {get;set;}
|
|
|
|
public string HashedPassword {get;set;}
|
|
|
|
public string Salt {get;set;}
|
|
|
|
|
|
const int iterations = 350000;
|
|
|
|
public string Password {
|
|
set{
|
|
if(!string.IsNullOrWhiteSpace(value))
|
|
{
|
|
byte[] salt = new byte[64];
|
|
var rand=RandomNumberGenerator.Create();
|
|
rand.GetBytes(salt);
|
|
Salt = Convert.ToBase64String(salt);
|
|
|
|
HashedPassword = PasswordHash(value);
|
|
}
|
|
|
|
}
|
|
}
|
|
private string PasswordHash(string password)
|
|
{
|
|
using(var alg=SHA512Managed.Create())
|
|
return Convert.ToBase64String(alg.ComputeHash(Encoding.UTF8.GetBytes($"{password}{Salt}")));
|
|
}
|
|
public bool IsCorrectPassword(string password)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(password)) return false;
|
|
return string.Equals(PasswordHash(password),HashedPassword);
|
|
}
|
|
}
|
|
} |