byte[] returned from ComputeHash() is a byte array and may not be
appropriately converted to a string with ASCII encoding. Try
comparing the byte arrays directly or using a different conversion
function.
We use a custom ToHex function (below) for creating strings from
hashed values (which is also how you often see them in specs and
documentation).
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
public static string ToHex(byte[] buff)
{
return ToHex(buff, true);
}
public static string ToHex(byte[] buff, bool lowerCase)
{
if (buff == null)
{
return null;
}
if (buff.Length == 0)
{
return String.Empty;
}
StringBuilder hex = new StringBuilder(buff.Length * 2);
foreach(byte b in buff)
{
hex.Append(b.ToString(lowerCase ? "x2" : "X2"));
}
return hex.ToString();
}
Hi, Han and Keith,
I tried the following code in both .Net 1.1 and 2.0:
==
string s1="abc";
//convert string to byte[]
byte[] bh=Encoding.ASCII.GetBytes(s);
SHA512 sh = new SHA512Managed();
//calculate hash
byte[] r=sh.ComputeHash(bh);
//convert byte[] to string
string s2=Encoding.ASCII.GetString(r);
Response.Write("output="+s2);
==
Here you are the output:
***
.Net 1.1: output=]/5!az:LAsI. A1fzN )~" nfKUS!*'OA(6:<##~k=EMD#d
.Net 2.0: output=??5??az??AsI? A1??N??~? ???KU??!??*'O??6?<#????EMD#d
***
One thing in common is that they all contain 53 characters, but they are not same string.
Hi,
I use System.Security.Cryptography.HashAlgorithm.ComputeHash() method
with SHA512 to encrypt password.
I recently upgrade my website from .Net 1.1 to .Net 2.0. The passwords stop working.
Would you please tell me if the System.Security.Cryptography.HashAlgorithm.ComputeHash()
generate exact same hash code in both versions of .Net for the same given byte[]?
If the method in 2 versions indeed generate different hash code,
Thank you
hb