views:

32

answers:

1

I understand that System.Security.Cryptography has a MD5 hashing method in MD5.ComputeHash. However, the method takes and returns bytes. I don't understand how to work with this method using String key and hashes. I try to work around by doing this,

var hash = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(@"text".ToCharArray()));
foreach(byte h in hash)
{
    Console.Write((char)h);
}

However the resulting output is gibberish string. For comparison, in this website, entering "text" will result in "1cb251ec0d568de6a929b520c4aed8d1"

A: 

writing this code will give the same result as the website:

var hash = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(@"text".ToCharArray()));
foreach(byte h in hash)
{
     Console.Write(h.ToString("x2"));
}

The trick is to print each byte as 2 hexadecimal digits (hence x2)

Louis Rhys