ace/Source/ACE.Database/Models/Auth/AccountExtensions.cs
Mag-nus a162905b1c WIP: Switch Database system to Entity Framework Core, DB First (#633)
* AuthenticationDatabase switched to Entity Framework Core, DB First

* Adds AsNoTracking() for performance and implements CreateAccount() instead of AddAccount()

* woops

* Auth db no longer needs to be init, or inherit from Database

* test fix hopefully

* Database objects in ACE.Entity would no longer be required

* Major WIP

* more progress

* AuthenticationDatabase switched to Entity Framework Core, DB First

* Adds AsNoTracking() for performance and implements CreateAccount() instead of AddAccount()

* woops

* Auth db no longer needs to be init, or inherit from Database

* test fix hopefully

* Database objects in ACE.Entity would no longer be required

* Major WIP

* more progress

* include missing CharacterCreateInfo

* GetCharacters changes to show how we can use the propertybag for these values

* Lot of progress with the WorldObject code

* comment add

* add biota to CreateWorldObject for loading inv from db
2018-03-06 04:35:48 -05:00

47 lines
1.5 KiB
C#

using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace ACE.Database.Models.Auth
{
public static class AccountExtensions
{
/// <summary>
/// creates a new account object and pre-creates a new, random salt
/// </summary>
public static void CreateRandomSalt(this Account account)
{
byte[] salt = new byte[64]; // 64 bytes = 512 bits, ideal for use with SHA512
using (var salter = new RNGCryptoServiceProvider())
salter.GetNonZeroBytes(salt);
account.PasswordSalt = Convert.ToBase64String(salt);
}
public static bool PasswordMatches(this Account account, string password)
{
var input = GetPasswordHash(account, password);
return input == account.PasswordHash;
}
public static void SetPassword(this Account account, string value)
{
account.PasswordHash = GetPasswordHash(account, value);
}
private static string GetPasswordHash(Account account, string password)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] saltBytes = Convert.FromBase64String(account.PasswordSalt);
byte[] buffer = passwordBytes.Concat(saltBytes).ToArray();
byte[] hash;
using (SHA512Managed hasher = new SHA512Managed())
hash = hasher.ComputeHash(buffer);
return Convert.ToBase64String(hash);
}
}
}