diff --git a/csharp-password-hash/csharp-password-hash/Hashing.cs b/csharp-password-hash/csharp-password-hash/Hashing.cs index 8405c4a..e669920 100644 --- a/csharp-password-hash/csharp-password-hash/Hashing.cs +++ b/csharp-password-hash/csharp-password-hash/Hashing.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Security.Cryptography; using System.Text; +using HashLib; namespace CSharpPasswordHash { @@ -88,6 +89,7 @@ public static string HashPassword(string password, string salt, HashingAlgo hash HashingAlgo.SHA512 => ToHashAlgorithm(SHA512.Create(), Encoding.UTF8.GetBytes(password), encodingType), HashingAlgo.MD5 => ToHashAlgorithm(MD5.Create(), Encoding.ASCII.GetBytes(password), encodingType), HashingAlgo.PBKDF2 => ToPBKDF2(password, salt, encodingType, pbkdf2Iterations), + HashingAlgo.MD2 => CreateMd2(password, encodingType), HashingAlgo.NONE => password, _ => throw new ArgumentOutOfRangeException(nameof(hashingAlgo)) }; @@ -105,6 +107,7 @@ public static bool CheckPassword(string password, string salt, string hash, Hash HashingAlgo.SHA512 => ToHashAlgorithm(SHA512.Create(), Encoding.UTF8.GetBytes(password), encodingType) == hash, HashingAlgo.MD5 => ToHashAlgorithm(MD5.Create(), Encoding.ASCII.GetBytes(password), encodingType) == hash, HashingAlgo.PBKDF2 => ToPBKDF2(password, salt, encodingType, pbdfk2Iterations) == hash, + HashingAlgo.MD2 => CreateMd2(password,encodingType) == hash, HashingAlgo.NONE => false, _ => throw new ArgumentOutOfRangeException(nameof(hashingAlgo)) }; @@ -119,6 +122,29 @@ public static string GenerateSalt() .Select(s => s[random.Next(s.Length)]) .ToArray()); } + private static string CreateMd2(string inputValue, EncodingType encodingType) + { + byte[] inputValueBytes = Encoding.ASCII.GetBytes(inputValue); + return CreateMd2(inputValueBytes, encodingType); + } + private static string CreateMd2(byte[] inputValueBytes, EncodingType encodingType) + { + var hash = HashFactory.Crypto.CreateMD2(); + var hashResult = hash.ComputeBytes(inputValueBytes); + var hashBytes = hashResult.GetBytes(); + + switch (encodingType) + { + case EncodingType.Default: + return ConvertToHex(hashBytes); + case EncodingType.Base64: + return Convert.ToBase64String(hashBytes); + case EncodingType.UTF8: + return Encoding.UTF8.GetString(hashBytes); + default: + return ConvertToHex(hashBytes); + } + } public static (HashingAlgo hashingAlgo, EncodingType encodingType) GetAlgoDet(string password, string salt, string hash) { diff --git a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs index fe6b899..a5b633d 100644 --- a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs +++ b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs @@ -9,6 +9,7 @@ public enum HashingAlgo SHA256 = 4, HMAC_SHA256 = 5, SHA512 = 6, - PBKDF2 = 7 - } + PBKDF2 = 7, + MD2=8 +} } diff --git a/csharp-password-hash/csharp-password-hash/csharp-password-hash.csproj b/csharp-password-hash/csharp-password-hash/csharp-password-hash.csproj index 49b463e..b84ce55 100644 --- a/csharp-password-hash/csharp-password-hash/csharp-password-hash.csproj +++ b/csharp-password-hash/csharp-password-hash/csharp-password-hash.csproj @@ -7,6 +7,7 @@ +