A
Adrian Grigorof
Hi all,
I am trying to generate an MD5 hash for a string (ABC) but I am getting a
different hash when I use the C# program below vs. a script I wrote in Perl
or other MD5 programs. Here is the relevant C# code:
string sData = "ABC";
string sResult = "";
Byte[] data1ToHash = ConvertStringToByteArray(sData);
byte[] hashvalue1 = ((HashAlgorithm)
CryptoConfig.CreateFromName("MD5")).ComputeHash(data1ToHash);
sResult = BitConverter.ToString(hashvalue1);
public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}
The resulting hash is:
71-67-74-B5-3A-92-B9-85-25-BB-A1-79-A1-59-CC-71
while the other programs show:
90-2F-BD-D2-B1-DF-0C-4F-70-B4-A5-D2-35-25-E9-32
See for example this online MD5 JavaScript:
http://pajhome.org.uk/crypt/md5/. Here is the Perl script I wrote
(generating the same hash as the online JavaScript):
$u = utf8("ABC");
$md5 = new MD5;
$md5->add($u);
$digest = $md5->digest();
$Hash = unpack("H*", $digest);
print "Hash: $Hash\n";
What I am doing wrong? Is it the Unicode encoding (UTF-16 vs. UTF-8)? If
yes, do I have to transform the string to Unicode before being able to use
the MD5 class?
I am trying to generate an MD5 hash for a string (ABC) but I am getting a
different hash when I use the C# program below vs. a script I wrote in Perl
or other MD5 programs. Here is the relevant C# code:
string sData = "ABC";
string sResult = "";
Byte[] data1ToHash = ConvertStringToByteArray(sData);
byte[] hashvalue1 = ((HashAlgorithm)
CryptoConfig.CreateFromName("MD5")).ComputeHash(data1ToHash);
sResult = BitConverter.ToString(hashvalue1);
public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}
The resulting hash is:
71-67-74-B5-3A-92-B9-85-25-BB-A1-79-A1-59-CC-71
while the other programs show:
90-2F-BD-D2-B1-DF-0C-4F-70-B4-A5-D2-35-25-E9-32
See for example this online MD5 JavaScript:
http://pajhome.org.uk/crypt/md5/. Here is the Perl script I wrote
(generating the same hash as the online JavaScript):
$u = utf8("ABC");
$md5 = new MD5;
$md5->add($u);
$digest = $md5->digest();
$Hash = unpack("H*", $digest);
print "Hash: $Hash\n";
What I am doing wrong? Is it the Unicode encoding (UTF-16 vs. UTF-8)? If
yes, do I have to transform the string to Unicode before being able to use
the MD5 class?