SHA256 哈希的十六进制输入
Hex input to SHA256 hash
可以使用 SHA256 从该站点获取在线哈希码。
https://emn178.github.io/online-tools/sha256.html
1的哈希码是“6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b”
但如果我们将哈希码输入类型设为 1 hex,则会出现“4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a”
我可以用 c# 代码完成第一个,但找不到可以完成第二个的 c# 代码。你能帮忙吗?
static string ComputeSha256Hash(string rawData)
{
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
请试试这个,关键是convert hex to bytes。
public static byte[] StringToByteArray(string hex)
{
int len = hex.Length;
//the Whosebug answer assumes you have even length,
//so I insert a 0 if odd length.
if (len % 2 == 1)
{
hex = hex.Insert(0, "0");
}
return Enumerable.Range(0, len)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
static string ComputeSha256Hash(string rawData)
{
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(StringToByteArray(rawData));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
static void Main(string[] args)
{
string hash = ComputeSha256Hash("1");
Console.WriteLine(hash);
Console.ReadKey();
}
可以使用 SHA256 从该站点获取在线哈希码。
https://emn178.github.io/online-tools/sha256.html
1的哈希码是“6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b”
但如果我们将哈希码输入类型设为 1 hex,则会出现“4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a”
我可以用 c# 代码完成第一个,但找不到可以完成第二个的 c# 代码。你能帮忙吗?
static string ComputeSha256Hash(string rawData)
{
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
请试试这个,关键是convert hex to bytes。
public static byte[] StringToByteArray(string hex)
{
int len = hex.Length;
//the Whosebug answer assumes you have even length,
//so I insert a 0 if odd length.
if (len % 2 == 1)
{
hex = hex.Insert(0, "0");
}
return Enumerable.Range(0, len)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
static string ComputeSha256Hash(string rawData)
{
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(StringToByteArray(rawData));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
static void Main(string[] args)
{
string hash = ComputeSha256Hash("1");
Console.WriteLine(hash);
Console.ReadKey();
}