From f6794a09a39d8dfa000c1c94a85a21d38bb559e2 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sat, 5 Jun 2021 10:48:33 +0200 Subject: [PATCH] Update Security methods --- Akari.Prototype.Server/Utils/Security.cs | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Akari.Prototype.Server/Utils/Security.cs b/Akari.Prototype.Server/Utils/Security.cs index 581b6ce..49e3f40 100644 --- a/Akari.Prototype.Server/Utils/Security.cs +++ b/Akari.Prototype.Server/Utils/Security.cs @@ -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 Argon2idDeriveBytes(byte[] password, byte[] salt, int length, bool clear = false, int? threads = null) {