将 md5 哈希字节数组转换为字符串

发布于 2024-08-24 22:30:14 字数 249 浏览 9 评论 0原文

如何将哈希结果(字节数组)转换为字符串?

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

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

我需要将 byteHashedPassword 转换为字符串。

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

I need to convert byteHashedPassword to a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我的黑色迷你裙 2024-08-31 22:30:14
   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();
    }

然后您可以将其作为扩展方法调用:

string hexString = byteArray.ToHex(false);
   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);
意犹 2024-08-31 22:30:14

我总是发现这是最方便的:

string hashPassword = BitConverter.ToString(byteHashedPassword).Replace("-","");

出于某种奇怪的原因,BitConverter 喜欢在字节之间放置破折号,因此替换只是删除它们。

更新:
如果您更喜欢“小写”十六进制,只需执行 .ToLower() 即可。

请注意,如果您将其作为紧密循环和许多操作来执行,则这可能会很昂贵,因为至少有两个隐式字符串转换和大小调整正在进行。

I always found this to be the most convenient:

string hashPassword = BitConverter.ToString(byteHashedPassword).Replace("-","");

For some odd reason BitConverter likes to put dashes between bytes, so the replace just removes them.

Update:
If you prefer "lowercase" hex, just do a .ToLower() and boom.

Do note that if you are doing this as a tight loop and many ops this could be expensive since there are at least two implicit string casts and resizes going on.

|煩躁 2024-08-31 22:30:14

您可以使用 Convert.ToBase64StringConvert.FromBase64String 轻松将字节数组转换为字符串。

You can use Convert.ToBase64String and Convert.FromBase64String to easily convert byte arrays into strings.

小猫一只 2024-08-31 22:30:14

如果您属于“十六进制偏好”阵营,您可以这样做。这基本上是 Philippe Leybaert 答案的最小版本。

string.Concat(hash.Select(x => x.ToString("X2")))

B1DB2CC0BAEE67EA47CFAEDBF2D747DF

If you're in the 'Hex preference' camp you can do this. This is basically a minimal version of the answer by Philippe Leybaert.

string.Concat(hash.Select(x => x.ToString("X2")))

B1DB2CC0BAEE67EA47CFAEDBF2D747DF

白云不回头 2024-08-31 22:30:14

由于它是一个哈希值,因此它可能具有无法在普通字符串中显示的值,因此最好的选择是将其转换为 Base64 编码的字符串。

string s = Convert.ToBase64String(bytes);

并用于

byte[] bytes = Convert.FromBase64(s);

取回字节。

Well as it is a hash, it has possibly values that cannot be shown in a normal string, so the best bet is to convert it to Base64 encoded string.

string s = Convert.ToBase64String(bytes);

and use

byte[] bytes = Convert.FromBase64(s);

to get the bytes back.

一萌ing 2024-08-31 22:30:14

,您可以使用接受字节和编码的字符串构造函数,但是您可能会得到一个难以管理的字符串,因为它可能包含很多有趣的字符(空字节、换行符、控制字符等)

好吧 执行此操作的方法是使用 Base 64 对其进行编码,以获得易于使用的漂亮字符串:

string s = Convert.ToBase64String(bytes);

并从该字符串返回字节数组:

byte[] bytes = Convert.FromBase64String(s);

Well, you could use the string constructor that takes bytes and an encoding, but you'll likely get a difficult to manage string out of that since it could contain lots of fun characters (null bytes, newlines, control chars, etc)

The best way to do this would be to encode it with base 64 to get a nice string that's easy to work with:

string s = Convert.ToBase64String(bytes);

And to go from that string back to a byte array:

byte[] bytes = Convert.FromBase64String(s);
泪冰清 2024-08-31 22:30:14

我知道这是一个老问题,但我想我会添加你可以使用内置方法(我正在使用.NET 6.0):

  • Convert.ToBase64String(hashBytes); (其他人已经提到了这一点)
  • Convert.ToHexString(hashBytes);

I know this is an old question, but I thought I would add you can use the built-in methods (I'm using .NET 6.0):

  • Convert.ToBase64String(hashBytes); (Others have already mentioned this)
  • Convert.ToHexString(hashBytes);
谜兔 2024-08-31 22:30:14

对于任何感兴趣的人,我创建的名为 CryptoStringify 的 Nuget 包允许您使用干净的语法将字符串转换为散列字符串,而无需使用字节数组:

using (MD5 md5 = MD5.Create())
{
    string strHashedPassword = md5.Hash(password);
}

这是 上的扩展方法HashAlgorithmKeyedHashAlgorithm 也适用于 SHA1、HMACSHA1、SHA256 等。

https://www.nuget.org/packages/cryptostringify

For anyone interested a Nuget package I created called CryptoStringify allows you to convert a string to a hashed string using a nice clean syntax, without having to play around with byte arrays:

using (MD5 md5 = MD5.Create())
{
    string strHashedPassword = md5.Hash(password);
}

It's an extension method on HashAlgorithm and KeyedHashAlgorithm so works on SHA1, HMACSHA1, SHA256 etc too.

https://www.nuget.org/packages/cryptostringify

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文