[英]C# SHA256 Hashing
Below is the code I use for hashing and I am trying to store the hash in a string(strSHA256) with UTF-8 encoding.下面是我用于散列的代码,我试图将散列存储在一个带有 UTF-8 编码的字符串(strSHA256)中。
using (SHA256 sha256Hash = SHA256.Create())
{
bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(utf8EncodedString));
strSHA256 = Encoding.UTF8.GetString(bytes);
}
When I inspect the string variable I see " u Ou\ O zA \ \\0 \\a }\ L Dr " type of value.当我检查字符串变量时,我看到“ u Ou\ O zA \ \\0 \\a }\ L Dr "类型的值。 My question is regarding to
?
我的问题是关于
?
character marked in bold.字符以粗体标记。 They should be UTF-8 chars.
它们应该是 UTF-8 字符。 Why cant I view the UTF-8 special chars when I inspect from visual studio.
当我从 Visual Studio 进行检查时,为什么无法查看 UTF-8 特殊字符。 The string o/p contains this weird char as well.
字符串 o/p 也包含这个奇怪的字符。
Note: I tried SHA256 with Node Js and I tend to see perfect UTF-8 special chars.注意:我用 Node Js 尝试过 SHA256,我倾向于看到完美的 UTF-8 特殊字符。
As @user2864740 mentioned bytes are not UTF8.正如@user2864740 提到的字节不是 UTF8。 Use base64 for bytes string representation.
使用 base64 表示字节字符串。
var hello = "Hello world!";
using (var sha256Hash = SHA256.Create())
{
var bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(hello));
// String representation
var stringForBytes = Convert.ToBase64String(bytes);
// HEX string representation
var str = new StringBuilder(bytes.Length * 2);
foreach (var b in bytes)
{
str.Append(b.ToString("X2"));
}
var hexString = str.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.