Cleans up Argon2 (#141)

This commit is contained in:
Kamron Batman 2020-05-24 19:13:19 -07:00 committed by GitHub
parent 5d51e97c52
commit f957a1dd5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 17 additions and 39 deletions

View file

@ -102,10 +102,10 @@ namespace System.Security.Cryptography
);
[DllImport("libargon2", EntryPoint = "argon2_verify")]
internal static extern unsafe Argon2Error crypto_argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
internal static extern Argon2Error crypto_argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
[DllImport("libargon2", EntryPoint = "decode_string")]
internal static extern unsafe Argon2Error crypto_decode_string(Argon2Context ctx, in byte str, int type);
internal static extern Argon2Error crypto_decode_string(Argon2Context ctx, in byte str, int type);
}
}
}

View file

@ -2,15 +2,14 @@
<PropertyGroup>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<PackageVersion>1.1.8</PackageVersion>
<RootNamespace>Server</RootNamespace>
<RootNamespace>System.Security.Cryptography</RootNamespace>
<AssemblyName>Argon2.Bindings</AssemblyName>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>8.0</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>true</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<Configurations>Debug;Release;Analyze</Configurations>
<AssemblyVersion>1.1.8</AssemblyVersion>
<Platforms>x64</Platforms>

View file

@ -2,7 +2,7 @@
<package>
<metadata>
<id>Argon2.Bindings</id>
<version>1.1.6</version>
<version>1.1.8</version>
<authors>Kamron Batman</authors>
<owners>Kamron Batman</owners>
<license type="expression">MIT</license>

View file

@ -69,6 +69,7 @@
/// This will not be returned from the C# PasswordHasher wrapper
/// </summary>
DECODING_FAIL = -32,
/// <summary>
/// Unable to create the number of threads requested
/// </summary>

View file

@ -13,6 +13,6 @@
/// <param name="action">Which method the Argon2Exception originated from</param>
/// <param name="error">The error returned from the Argon2 library</param>
/// </summary>
public Argon2Exception(string action, Argon2Error error) : base(string.Format("Error during Argon2 {0}: ({1}) {2}", action, (int)error, error)) {}
public Argon2Exception(string action, Argon2Error error) : base($"Error during Argon2 {action}: ({(int)error}) {error}") {}
}
}

View file

@ -1,6 +1,5 @@
using System.Runtime.InteropServices;
using System.Text;
// using System.Text.RegularExpressions;
namespace System.Security.Cryptography
{
@ -10,10 +9,7 @@ namespace System.Security.Cryptography
/// </summary>
public class Argon2PasswordHasher
{
private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider();
// private static readonly Regex HashRegex = new Regex(@"^\$argon2([di])\$v=(\d+)$m=(\d+),t=(\d+),p=(\d+)\$([A-Za-z0-9+/=]+)\$([A-Za-z0-9+/=]*)$", RegexOptions.Compiled);
private static RandomNumberGenerator m_Rng;
/// <summary>
/// How many iterations of the Argon2 hash to perform
@ -48,13 +44,17 @@ namespace System.Security.Cryptography
/// </summary>
public Encoding StringEncoding { get; set; }
/// <summary>
/// Randomizer used to generate salts
/// </summary>
public RandomNumberGenerator Rng { get; set; }
/// <summary>
/// Initialize the Argon2 PasswordHasher with default performance and algorithm settings based upon the environment the hashing will be used in.
/// You should perform your own profiling to determine what the parameters should be for your specific usage; however, this attempts to provide
/// some reasonable defaults.
/// </summary>
public Argon2PasswordHasher()
public Argon2PasswordHasher(RandomNumberGenerator rng = null)
{
TimeCost = 3;
MemoryCost = 8192;
@ -62,28 +62,9 @@ namespace System.Security.Cryptography
ArgonType = Argon2Type.Argon2i;
HashLength = 32;
StringEncoding = Encoding.UTF8;
Rng = rng ?? (m_Rng ??= new RNGCryptoServiceProvider());
}
/// <summary>
/// Initialize the Argon2 PasswordHasher with the performance and algorithm settings to use while hashing
/// <param name="timeCost">How many iterations of the Argon2 hash to perform (default: 3, must be at least 1)</param>
/// <param name="memoryCost">How much memory to use while hashing in kibibytes (KiB) (default: 8192 KiB [8 MiB], must be at least 8 KiB)</param>
/// <param name="parallelism">How many threads to use while hashing (default: 1, must be at least 1)</param>
/// <param name="argonType">The type of Argon2 hashing algorithm to use (Independent [default] or Dependent)</param>
/// <param name="hashLength">The length of the resulting hash in bytes (default: 32)</param>
/// </summary>
public Argon2PasswordHasher(uint timeCost = 3, uint memoryCost = 8192, uint parallelism = 1, Argon2Type argonType = Argon2Type.Argon2i, uint hashLength = 32)
{
TimeCost = timeCost;
MemoryCost = memoryCost;
Parallelism = parallelism;
ArgonType = argonType;
HashLength = hashLength;
StringEncoding = Encoding.UTF8;
}
/// <summary>
/// Hash the password using Argon2 with a cryptographically-secure, random, 16-byte salt.
/// This is the only overload of the Hash method that the typical user will need to use for password storage. The other overloads are provided for interoperability purposes.

View file

@ -1,6 +1,4 @@
using System;
namespace System.Security.Cryptography
namespace System.Security.Cryptography
{
/// <summary>
/// HashMetadata represents the information stored in the encoded Argon2 format

View file

@ -19,7 +19,6 @@
*************************************************************************/
using System;
using System.Security.Cryptography;
namespace Server
{

View file

@ -3,7 +3,7 @@
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Argon2PasswordProtection.cs *
* Created: 2020/05/01 - Updated: 2020/05/02 *
* Created: 2020/05/01 - Updated: 2020/05/24 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
@ -26,7 +26,7 @@ namespace Server.Accounting.Security
public class Argon2PasswordProtection : IPasswordProtection
{
public static IPasswordProtection Instance = new Argon2PasswordProtection();
private Argon2PasswordHasher m_PasswordHasher = new Argon2PasswordHasher();
private Argon2PasswordHasher m_PasswordHasher = new Argon2PasswordHasher(RandomProviders.SecureProvider as RandomNumberGenerator);
public string EncryptPassword(string plainPassword) =>
m_PasswordHasher.Hash(plainPassword);