MD5 hash in silverlight

馋奶兔 提交于 2019-12-10 12:39:40

问题


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?


回答1:


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.)




回答2:


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.



来源:https://stackoverflow.com/questions/4206219/md5-hash-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!