This is a solution for SHA1 variant.
public static string GetSwcSHA1(string value)
{
SHA1 algorithm = SHA1.Create();
byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
string sh1 = "";
for (int i = 0; i < data.Length; i++)
{
sh1 += data[i].ToString("x2").ToUpperInvariant();
}
return sh1;
}
For MD5 you only need to change the algorithm to:
MD5 algorithm = MD5.Create();
Hope you don't mind, just going to add a VB.NET variant of your code above:
Public Shared Function CreateHash(saltAndPassword) As String
Dim Algorithm As SHA1 = SHA1.Create()
Dim Data As Byte() = Algorithm.ComputeHash(Encoding.UTF8.GetBytes(saltAndPassword))
Dim Hashed As String = ""
For i As Integer = 0 To Data.Length - 1
Hashed &= Data(i).ToString("x2").ToUpperInvariant()
Next
Return Hashed
End Function
Whats the recommended way forward for issues like this? Continuing to use "obsolete" calls is obviously not the suggested path, so has it been replaced by something else other than "just use the membership APIs"?
The best way (which you've ruled out) purely within the .NET Framework is to change everything over to have the passwords hashed by PBKDF2, Bcrypt, or Scrypt. PBKDF2 is provided in .NET by the Rfc2898DeriveBytes Class.
The second best way is to end up with two "versions" of passwords:
The third best way is the second best way, but with only version 1. Be careful, this way lies DCC2 madness - you keep wrapping your old output inside newer algorithms
In both cases, you'll be storing PBKDF2-HMAC-SHA-1 results in the database, so you'll need:
P.S. for the Version 1 or Version 2 newer algorith, Jither created a .NET library capable of PBKDF2-HMAC-SHA256, PBKDF2-HMAC-SHA512, and so on; my Github repository contains a variant of it with a reasonable set of test vectors.
Why cannot use the simplest one by .Net
public static string HashString(string inputString, string hashName)
{
var algorithm = HashAlgorithm.Create(hashName);
if (algorithm == null)
throw new ArgumentException("Unrecognized hash name", hashName);
byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
return Convert.ToBase64String(hash);
}
Richard's answer worked for me great. This is the code de-compiled from .NET Framework 4.5. If anyone thing is it better please use it. I guess it may be a bit faster.
public static string BinaryToHex(byte[] data)
{
if (data == null)
{
return null;
}
char[] hex = new char[checked((int)data.Length * 2)];
for (int i = 0; i < (int)data.Length; i++)
{
byte num = data[i];
hex[2 * i] = NibbleToHex((byte)(num >> 4));
hex[2 * i + 1] = NibbleToHex((byte)(num & 15));
}
return new string(hex);
}
private static char NibbleToHex(byte nibble)
{
int aChar = (nibble < 10 ? nibble + 48 : nibble - 10 + 65);
return (char)aChar;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With