Begin script abstraction
This commit is contained in:
40
ReallifeGamemode.Server.Common/PasswordHasher.cs
Normal file
40
ReallifeGamemode.Server.Common/PasswordHasher.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace ReallifeGamemode.Server.Common
|
||||
{
|
||||
public class PasswordHasher
|
||||
{
|
||||
public static byte[] GetNewSalt(int length = 256)
|
||||
{
|
||||
if ((length % 8) != 0)
|
||||
{
|
||||
throw new ArgumentException("Length mus be completely divisble through 8");
|
||||
}
|
||||
|
||||
var salt = new byte[length / 8];
|
||||
|
||||
using (var rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
rng.GetBytes(salt);
|
||||
}
|
||||
|
||||
return salt;
|
||||
}
|
||||
|
||||
public static string HashPassword(string password, byte[] salt, int length = 512)
|
||||
{
|
||||
if ((length % 8) != 0)
|
||||
{
|
||||
throw new ArgumentException("Length mus be completely divisble through 8");
|
||||
}
|
||||
|
||||
var hashedPassword = KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA512, 50000, length / 8);
|
||||
|
||||
return Convert.ToBase64String(hashedPassword);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user