I am working on a Windows phone 7 application. I am using this implementation for MD5 hashing in silverlight.
I am using this code -
protected string GetMD5Hash(string input)
{
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
MD5Managed md5 = new MD5Managed();
byte[] hash = md5.ComputeHash(bs);
StringBuilder sb = new StringBuilder();
foreach (byte b in bs)
{
sb.Append(b.ToString("x2").ToLower());
}
return sb.ToString();
}
But, I am not getting the correct MD5 hash for the input I provide. I am not sure what is wrong with this code. If anyone has used this implementation for MD5 hashing in silverlight, do you know where have I gone wrong?
You're returning the hex version of the input, not the hash:
foreach (byte b in bs)
should be
foreach (byte b in hash)
(An alternative is to use Convert.ToBase64String(hash)
if you don't mind it being in Base64 rather than hex.)
There is already an accepted answer for this, but for others who are using MD5 in Silverlight or Windows Phone, I'm posting a link to another implementation of MD5 that I've had more success with.
I spent several hours beating my head against the wall with the implementation mentioned by the original post, trying to get it working in my Windows Phone project. It was working in some cases and not in others.
Jeff Wilcox's version worked perfectly.
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