Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions csharp-password-hash/csharp-password-hash/Hashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using HashLib;

namespace CSharpPasswordHash
{
Expand Down Expand Up @@ -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))
};
Expand All @@ -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))
};
Expand All @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions csharp-password-hash/csharp-password-hash/HashingAlgo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum HashingAlgo
SHA256 = 4,
HMAC_SHA256 = 5,
SHA512 = 6,
PBKDF2 = 7
}
PBKDF2 = 7,
MD2=8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HashLib" Version="2.0.1" />
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>
</Project>