将 md5 哈希字节数组转换为字符串
如何将哈希结果(字节数组)转换为字符串?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
然后您可以将其作为扩展方法调用:
You can then call it as an extension method:
我总是发现这是最方便的:
出于某种奇怪的原因,BitConverter 喜欢在字节之间放置破折号,因此替换只是删除它们。
更新:
如果您更喜欢“小写”十六进制,只需执行 .ToLower() 即可。
请注意,如果您将其作为紧密循环和许多操作来执行,则这可能会很昂贵,因为至少有两个隐式字符串转换和大小调整正在进行。
I always found this to be the most convenient:
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.
您可以使用
Convert.ToBase64String
和Convert.FromBase64String
轻松将字节数组转换为字符串。You can use
Convert.ToBase64String
andConvert.FromBase64String
to easily convert byte arrays into strings.如果您属于“十六进制偏好”阵营,您可以这样做。这基本上是 Philippe Leybaert 答案的最小版本。
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.
B1DB2CC0BAEE67EA47CFAEDBF2D747DF
由于它是一个哈希值,因此它可能具有无法在普通字符串中显示的值,因此最好的选择是将其转换为 Base64 编码的字符串。
并用于
取回字节。
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.
and use
to get the bytes back.
,您可以使用接受字节和编码的字符串构造函数,但是您可能会得到一个难以管理的字符串,因为它可能包含很多有趣的字符(空字节、换行符、控制字符等)
好吧 执行此操作的方法是使用 Base 64 对其进行编码,以获得易于使用的漂亮字符串:
并从该字符串返回字节数组:
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:
And to go from that string back to a byte array:
我知道这是一个老问题,但我想我会添加你可以使用内置方法(我正在使用.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);
对于任何感兴趣的人,我创建的名为 CryptoStringify 的 Nuget 包允许您使用干净的语法将字符串转换为散列字符串,而无需使用字节数组:
这是
上的扩展方法HashAlgorithm
和KeyedHashAlgorithm
也适用于 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:
It's an extension method on
HashAlgorithm
andKeyedHashAlgorithm
so works on SHA1, HMACSHA1, SHA256 etc too.https://www.nuget.org/packages/cryptostringify