From ab860613307c8a82c7ec92370f01c7a199c6c9e4 Mon Sep 17 00:00:00 2001 From: hemant404 <56862469+hemant404@users.noreply.github.com> Date: Thu, 1 Oct 2020 17:47:34 +0530 Subject: [PATCH] title : Added support for MD2 hashing algo. --- .../csharp-password-hash/Hashing.cs | 30 +++++++++++++++++++ .../csharp-password-hash/HashingAlgo.cs | 3 +- .../csharp-password-hash.csproj | 4 +++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/csharp-password-hash/csharp-password-hash/Hashing.cs b/csharp-password-hash/csharp-password-hash/Hashing.cs index 396ad91..da73798 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 { @@ -140,6 +141,9 @@ public static string EncryptPassword(string password, string salt, HashingAlgo h case HashingAlgo.MD5: return ToMd5(password, encodingType); + case HashingAlgo.MD2: + return CreateMd2(password, encodingType); + default: throw new ArgumentOutOfRangeException(nameof(hashingAlgo)); } @@ -160,6 +164,9 @@ public static bool CheckPassword(string password, string salt, string hash, Hash return ToSHA256(password, encodingType) == hash; case HashingAlgo.MD5: return ToMd5(password, encodingType) == hash; + case HashingAlgo.MD2: + return CreateMd2(password, encodingType)==hash; + default: throw new ArgumentOutOfRangeException(nameof(hashingAlgo)); } @@ -174,6 +181,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); + } + } } } diff --git a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs index 1472435..730b528 100644 --- a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs +++ b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs @@ -6,6 +6,7 @@ public enum HashingAlgo MD5 = 1, SHA1 = 2, SHA256 = 3, - HMAC_SHA256 = 4 + HMAC_SHA256 = 4, + MD2=5 } } \ No newline at end of file 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 cb20835..407c392 100644 --- a/csharp-password-hash/csharp-password-hash/csharp-password-hash.csproj +++ b/csharp-password-hash/csharp-password-hash/csharp-password-hash.csproj @@ -5,4 +5,8 @@ CSharpPasswordHash + + + +