简体   繁体   English

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

[英]Converting a md5 hash byte array to a string

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. 我需要将byteHashedPassword转换为字符串。

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

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. 出于某种奇怪的原因,BitConverter喜欢将破折号放在字节之间,因此替换操作将其删除。

Update: If you prefer "lowercase" hex, just do a .ToLower() and boom. 更新:如果您希望使用“小写”十六进制,只需执行.ToLower()和繁荣。

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. 请注意,如果您这样做是一个紧密循环,并且很多操作都可能会很昂贵,因为至少要进行两次隐式字符串强制转换和调整大小。

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

If you're in the 'Hex preference' camp you can do this. 如果您处于“十六进制偏好”阵营,则可以执行此操作。 This is basically a minimal version of the answer by Philippe Leybaert. 这基本上是Philippe Leybaert的答案的最低版本。

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

B1DB2CC0BAEE67EA47CFAEDBF2D747DF

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. 由于它是一个散列,因此它可能具有无法在普通字符串中显示的值,因此最好的选择是将其转换为Base64编码的字符串。

string s = Convert.ToBase64String(bytes);

and use 和使用

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

to get the bytes back. 取回字节。

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: 最好的方法是使用base 64进行编码,以获得易于使用的漂亮字符串:

string s = Convert.ToBase64String(bytes);

And to go from that string back to a byte array: 并从该字符串返回字节数组:

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

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: 对于感兴趣的任何人,我创建的一个名为CryptoStringify的Nuget包都可以让您使用简洁的语法将字符串转换为哈希字符串,而无需使用字节数组:

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. 它是HashAlgorithmKeyedHashAlgorithm的扩展方法,因此也适用于SHA1,HMACSHA1,SHA256等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM