MD5哈希在Silverlight [英] MD5 hash in silverlight

查看:113
本文介绍了MD5哈希在Silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在Windows Phone 7应用程序。我使用为MD5在Silverlight哈希此实现

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();    
    }



不过,我没有得到为输入我提供正确的MD5哈希值。我不知道什么是错的这个代码。如果有人使用了的MD5在Silverlight哈希此实现,你知道我在哪里出了错?

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)

foreach (byte b in hash)

(另一种方法是使用 Convert.ToBase64String(散),如果你不介意它为Base64是相当比十六进制)。

(An alternative is to use Convert.ToBase64String(hash) if you don't mind it being in Base64 rather than hex.)

这篇关于MD5哈希在Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆