Files
Akari.Prototype/Akari.Prototype.Server/Utils/Security.cs
Eveldee 2b81f3e5ba Add AuthLifetimeService
Refactor AuthManager
2021-06-04 18:32:47 +02:00

67 lines
2.0 KiB
C#

using Isopoh.Cryptography.Argon2;
using Isopoh.Cryptography.SecureArray;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Utils
{
public static class Security
{
public const int HashLength = 32;
public const int SaltLength = 12;
public static string Argon2idHash(byte[] password)
{
var salt = new byte[8];
RandomNumberGenerator.Fill(salt);
var config = new Argon2Config()
{
HashLength = 32,
Lanes = Environment.ProcessorCount / 2,
Threads = Environment.ProcessorCount / 2,
Password = password,
Salt = salt,
Type = Argon2Type.HybridAddressing,
Version = Argon2Version.Nineteen
};
return Argon2.Hash(config);
}
public static SecureArray<byte> Argon2idDeriveBytes(byte[] password, byte[] salt, int length, bool clear = false, int? threads = null)
{
int t = threads ?? Environment.ProcessorCount / 2;
if (t < 1)
{
t = 1;
}
var config = new Argon2Config()
{
HashLength = length,
Password = password,
Salt = salt,
Lanes = t,
Threads = t,
ClearPassword = clear,
ClearSecret = clear,
Type = Argon2Type.HybridAddressing,
Version = Argon2Version.Nineteen
};
return new Argon2(config).Hash();
}
public static SecureArray<byte> Argon2idDeriveBytes(string password, string salt, int length, bool clear = false, int? threads = null)
{
return Argon2idDeriveBytes(Encoding.UTF8.GetBytes(password), Encoding.UTF8.GetBytes(salt), length, clear, threads);
}
}
}