tesses-backup/TessesDedup/Account.cs

48 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace TessesDedup
{
public class Account
{
[JsonProperty("id")]
public long Id {get;set;}
[JsonProperty("username")]
public string Username {get;set;}
[JsonIgnore]
public string PasswordHash {get;set;}
[JsonIgnore]
public string PasswordSalt {get;set;}
public void NewSalt()
{
byte[] bytes=new byte[64];
using(var rnd = RandomNumberGenerator.Create())
rnd.GetBytes(bytes);
PasswordSalt = Convert.ToBase64String(bytes);
}
public string GetPasswordHash(string password)
{
string pass = $"{password}{PasswordSalt}";
using(var sha256Managed = SHA512.Create())
{
return Convert.ToBase64String(sha256Managed.ComputeHash(Encoding.UTF8.GetBytes(pass)));
}
}
public bool PasswordCorrect(string password)
{
string hash=GetPasswordHash(password);
return PasswordHash == hash ;
}
}
}