From d23995645cdfcf4fffb8953e8d0d82977daf516f Mon Sep 17 00:00:00 2001 From: Priyanka Hiranandani Date: Sun, 4 Oct 2020 23:03:10 +0530 Subject: [PATCH 1/6] Update PasswordHashing.cs --- .../csharp-password-hash/PasswordHashing.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/csharp-password-hash/csharp-password-hash/PasswordHashing.cs b/csharp-password-hash/csharp-password-hash/PasswordHashing.cs index 13d17f2..bc55a5e 100644 --- a/csharp-password-hash/csharp-password-hash/PasswordHashing.cs +++ b/csharp-password-hash/csharp-password-hash/PasswordHashing.cs @@ -65,5 +65,32 @@ public string GetHash(string password, HashingConfig hashConfig) return hashConfig.GenratePerPasswordSalt ? $"{passwordHash}:{salt}" : passwordHash; } + + public static (HashingAlgo hashingAlgo, EncodingType encodingType) GetAlgoDet(string password, string salt, string hash ) + { + var isValidAlgo=false; + var result = (hashingAlgo: HashingAlgo.NONE, encodingType: EncodingType.Default); + foreach (string algo in Enum.GetNames(typeof(HashingAlgo))) + { + HashingAlgo hashalgo = (HashingAlgo)Enum.Parse(typeof(HashingAlgo), algo); + + foreach (string encodeType in Enum.GetNames(typeof(EncodingType))) + { + EncodingType encodingType = (EncodingType)Enum.Parse(typeof(EncodingType), encodeType); + + isValidAlgo = CheckPassword(password, salt, hash, hashalgo, encodingType); + + if (isValidAlgo == true) + { + result.hashingAlgo = hashalgo; + result.encodingType = encodingType; + return result; + } + + } + + } + return result; + } } -} \ No newline at end of file +} From 736417cf5f57f38339ae85c466f893b0868e5d2e Mon Sep 17 00:00:00 2001 From: Priyanka Hiranandani Date: Sun, 4 Oct 2020 23:06:30 +0530 Subject: [PATCH 2/6] Update PasswordHashing.cs --- .../csharp-password-hash/PasswordHashing.cs | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/csharp-password-hash/csharp-password-hash/PasswordHashing.cs b/csharp-password-hash/csharp-password-hash/PasswordHashing.cs index bc55a5e..13fb5bb 100644 --- a/csharp-password-hash/csharp-password-hash/PasswordHashing.cs +++ b/csharp-password-hash/csharp-password-hash/PasswordHashing.cs @@ -66,31 +66,15 @@ public string GetHash(string password, HashingConfig hashConfig) return hashConfig.GenratePerPasswordSalt ? $"{passwordHash}:{salt}" : passwordHash; } - public static (HashingAlgo hashingAlgo, EncodingType encodingType) GetAlgoDet(string password, string salt, string hash ) - { - var isValidAlgo=false; - var result = (hashingAlgo: HashingAlgo.NONE, encodingType: EncodingType.Default); - foreach (string algo in Enum.GetNames(typeof(HashingAlgo))) - { - HashingAlgo hashalgo = (HashingAlgo)Enum.Parse(typeof(HashingAlgo), algo); - - foreach (string encodeType in Enum.GetNames(typeof(EncodingType))) - { - EncodingType encodingType = (EncodingType)Enum.Parse(typeof(EncodingType), encodeType); - - isValidAlgo = CheckPassword(password, salt, hash, hashalgo, encodingType); - - if (isValidAlgo == true) - { - result.hashingAlgo = hashalgo; - result.encodingType = encodingType; - return result; - } - - } + public HashingConfig GetPossibleConfig(string password, string salt, string saltedPasswordFormat, string inputhash) + { + var hashConfig = new HashingConfig (); + var saltedPassword = GetSaltedPassword(password, salt, saltedPasswordFormat); - } - return result; + var algoDet = Hashing.GetAlgoDet(saltedPassword, salt, inputhash); + hashConfig.HashingAlgo = algoDet.hashingAlgo; + hashConfig.PasswordHashEncodingType = algoDet.encodingType; + return hashConfig; } } } From c3ff4c9a617eea7c7a0f7d2a1e6b6d6e104fd2be Mon Sep 17 00:00:00 2001 From: Priyanka Hiranandani Date: Sun, 4 Oct 2020 23:08:46 +0530 Subject: [PATCH 3/6] Update Hashing.cs --- .../csharp-password-hash/Hashing.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/csharp-password-hash/csharp-password-hash/Hashing.cs b/csharp-password-hash/csharp-password-hash/Hashing.cs index 4de1273..dbd435a 100644 --- a/csharp-password-hash/csharp-password-hash/Hashing.cs +++ b/csharp-password-hash/csharp-password-hash/Hashing.cs @@ -210,6 +210,9 @@ public static string HashPassword(string password, string salt, HashingAlgo hash case HashingAlgo.PBKDF2: return ToPBKDF2(password, salt, encodingType, pbkdf2Iterations); + case HashingAlgo.NONE: + return password; + default: throw new ArgumentOutOfRangeException(nameof(hashingAlgo)); } @@ -234,6 +237,8 @@ public static bool CheckPassword(string password, string salt, string hash, Hash return ToMd5(password, encodingType) == hash; case HashingAlgo.PBKDF2: return ToPBKDF2(password, salt, encodingType, pbdfk2Iterations) == hash; + case HashingAlgo.NONE: + return false; default: throw new ArgumentOutOfRangeException(nameof(hashingAlgo)); } @@ -248,6 +253,34 @@ public static string GenerateSalt() .Select(s => s[random.Next(s.Length)]) .ToArray()); } + + public static (HashingAlgo hashingAlgo, EncodingType encodingType) GetAlgoDet(string password, string salt, string hash ) + { + var isValidAlgo=false; + var result = (hashingAlgo: HashingAlgo.NONE, encodingType: EncodingType.Default); + foreach (string algo in Enum.GetNames(typeof(HashingAlgo))) + { + HashingAlgo hashalgo = (HashingAlgo)Enum.Parse(typeof(HashingAlgo), algo); + + foreach (string encodeType in Enum.GetNames(typeof(EncodingType))) + { + EncodingType encodingType = (EncodingType)Enum.Parse(typeof(EncodingType), encodeType); + + isValidAlgo = CheckPassword(password, salt, hash, hashalgo, encodingType); + + if (isValidAlgo == true) + { + result.hashingAlgo = hashalgo; + result.encodingType = encodingType; + return result; + } + + } + + } + return result; + } + } } From 463cc918b694eebc1e1b13c134a9ac6e888c2d81 Mon Sep 17 00:00:00 2001 From: Priyanka Hiranandani Date: Sun, 4 Oct 2020 23:09:32 +0530 Subject: [PATCH 4/6] Update HashingAlgo.cs --- .../csharp-password-hash/HashingAlgo.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs index 80d696f..fe6b899 100644 --- a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs +++ b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs @@ -2,12 +2,13 @@ { public enum HashingAlgo { - HMAC_SHA1 = 0, - MD5 = 1, - SHA1 = 2, - SHA256 = 3, - HMAC_SHA256 = 4, - SHA512 = 5, - PBKDF2 = 6 + NONE = 0, + HMAC_SHA1 = 1, + MD5 = 2, + SHA1 = 3, + SHA256 = 4, + HMAC_SHA256 = 5, + SHA512 = 6, + PBKDF2 = 7 } -} \ No newline at end of file +} From b464e49945d3e5ca38eb421ddf3dbf65823eeae3 Mon Sep 17 00:00:00 2001 From: Priyanka Hiranandani Date: Mon, 5 Oct 2020 17:26:04 +0530 Subject: [PATCH 5/6] Update HashingAlgo.cs --- csharp-password-hash/csharp-password-hash/HashingAlgo.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/csharp-password-hash/csharp-password-hash/HashingAlgo.cs b/csharp-password-hash/csharp-password-hash/HashingAlgo.cs index fe6b899..1f47522 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, + BCRYPT = 8 } } From 9912659d3147f6f437501997cab5e241c9c2f4df Mon Sep 17 00:00:00 2001 From: Priyanka Hiranandani Date: Tue, 6 Oct 2020 11:07:59 +0530 Subject: [PATCH 6/6] Update Hashing.cs --- .../csharp-password-hash/Hashing.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/csharp-password-hash/csharp-password-hash/Hashing.cs b/csharp-password-hash/csharp-password-hash/Hashing.cs index dbd435a..fb21c40 100644 --- a/csharp-password-hash/csharp-password-hash/Hashing.cs +++ b/csharp-password-hash/csharp-password-hash/Hashing.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Security.Cryptography; using System.Text; +using BCrypt.Net; namespace CSharpPasswordHash { @@ -184,6 +185,11 @@ public static string ToPBKDF2(byte[] plainTextBytes, String salt, EncodingType e } } + private static ToBCRYPT(string password, string salt) + { + return BCrypt.HashPassword(password, salt); + } + public static string HashPassword(string password, string salt, HashingAlgo hashingAlgo, EncodingType encodingType, int pbkdf2Iterations) { @@ -209,7 +215,10 @@ public static string HashPassword(string password, string salt, HashingAlgo hash case HashingAlgo.PBKDF2: return ToPBKDF2(password, salt, encodingType, pbkdf2Iterations); - + + case HashingAlgo.BCRYPT: + return ToBCRYPT(password, salt); + case HashingAlgo.NONE: return password; @@ -237,6 +246,8 @@ public static bool CheckPassword(string password, string salt, string hash, Hash return ToMd5(password, encodingType) == hash; case HashingAlgo.PBKDF2: return ToPBKDF2(password, salt, encodingType, pbdfk2Iterations) == hash; + case HashingAlgo.BCRYPT: + return ToBCRYPT(password, salt) == hash; case HashingAlgo.NONE: return false; default: