Update Security methods

This commit is contained in:
2021-06-05 10:48:33 +02:00
parent 187c320704
commit f6794a09a3

View File

@@ -11,28 +11,41 @@ namespace Akari.Prototype.Server.Utils
{
public static class Security
{
public const int HashLength = 32;
public const int SaltLength = 12;
public const int DefaultHashLength = 32;
public const int DefaultSaltLength = 12;
public static string Argon2idHash(byte[] password)
public static string NewArgon2idHash(byte[] password, int hashLength = DefaultHashLength, int saltLength = DefaultSaltLength, bool clear = false, int? threads = null)
{
var salt = new byte[8];
int t = threads ?? Environment.ProcessorCount / 2;
if (t < 1)
{
t = 1;
}
var salt = new byte[saltLength];
RandomNumberGenerator.Fill(salt);
var config = new Argon2Config()
{
HashLength = 32,
Lanes = Environment.ProcessorCount / 2,
Threads = Environment.ProcessorCount / 2,
HashLength = hashLength,
Password = password,
Salt = salt,
Lanes = t,
Threads = t,
ClearPassword = clear,
ClearSecret = clear,
Type = Argon2Type.HybridAddressing,
Version = Argon2Version.Nineteen
};
return Argon2.Hash(config);
}
public static string NewArgon2idHash(string password, int hashLength = DefaultHashLength, int saltLength = DefaultSaltLength, bool clear = false, int? threads = null)
{
return NewArgon2idHash(Encoding.UTF8.GetBytes(password), hashLength, saltLength, clear, threads);
}
public static SecureArray<byte> Argon2idDeriveBytes(byte[] password, byte[] salt, int length, bool clear = false, int? threads = null)
{