将md5哈希字节数组转换为字符串 [英] Converting a md5 hash byte array to a string

查看:159
本文介绍了将md5哈希字节数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个字节数组的散列结果转换成字符串?

How can I convert the hashed result, which is a byte array, to a string?

byte[] bytePassword = Encoding.UTF8.GetBytes(password);

using (MD5 md5 = MD5.Create())
{
    byte[] byteHashedPassword = md5.ComputeHash(bytePassword);
} 

我需要转换 byteHashedPassword 到一个字符串。

I need to convert byteHashedPassword to a string.

推荐答案

   public static string ToHex(this byte[] bytes, bool upperCase)
    {
        StringBuilder result = new StringBuilder(bytes.Length*2);

        for (int i = 0; i < bytes.Length; i++)
            result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));

        return result.ToString();
    }

然后,您可以将其称为扩展方法:

You can then call it as an extension method:

string hexString = byteArray.ToHex(false);

这篇关于将md5哈希字节数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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