在。net Core中使用SHA-1

时间:2022-10-08 18:23:14

I get strange results when hashing a string in dotnet core I have found this similar question: Computing SHA1 with ASP.NET Core and have found how to convert a byte array to string in .net core

当我在dotnet核心中对一个字符串进行哈希时,我发现了一个类似的问题:使用ASP计算SHA1。在。NET Core中找到了如何将字节数组转换成字符串的方法

this is my code:

这是我的代码:

private static string CalculateSha1(string text)
{
    var enc = Encoding.GetEncoding(65001); // utf-8 code page
    byte[] buffer = enc.GetBytes(text);

    var sha1 = System.Security.Cryptography.SHA1.Create();

    var hash = sha1.ComputeHash(buffer);

    return enc.GetString(hash);
}

and this is my test:

这是我的测试:

string test = "broodjepoep"; // forgive me

string shouldBe = "b2bc870e4ddf0e15486effd19026def2c8a54753"; // according to http://www.sha1-online.com/

string wouldBe = CalculateSha1(test);

System.Diagnostics.Debug.Assert(shouldBe.Equals(wouldBe));

output:

输出:

���M�Hn�ѐ&��ȥGS

����M Hn�ѐ&��ȥGS

在。net Core中使用SHA-1

I have the nuget package System.Security.Cryptography.Algorithms installed (v 4.3.0)

我有nuget包系统,security,密码学。算法安装(安装v 4.3.0)

Also tried with GetEncoding(0) to get the sys default coding. Also did not work.

还尝试使用GetEncoding(0)获得sys默认编码。也没有工作。

2 个解决方案

#1


3  

I'm not sure how 'SHA-1 Online' represents your hash, buts since it's a hash, it can contain characters that can't be represented in a (UTF8) string. I think you're better off using Convert.ToBase64String() to easily represent the byte-array hash in a string:

我不确定“SHA-1在线”是如何表示你的哈希,但由于它是一个散列,它可以包含不能用(UTF8)字符串表示的字符。我认为您最好使用Convert.ToBase64String()来轻松地表示字符串中的字节数组散列:

var hashString = Convert.ToBase64String(hash);

To convert it back to a byte-array, use Convert.FromBase64String():

要将它转换回字节数组,请使用convert . frombase64string ():

var bytes =  Convert.FromBase64String(hashString);

Also see: Converting a md5 hash byte array to a string. Which shows there a multiple ways to represent hash in a string. For example, hash.ToString("X") will use a hexadecimal representation.

参见:将md5散列字节数组转换为字符串。它显示了在字符串中表示哈希的多种方式。例如,hash.ToString(“X”)将使用十六进制表示。

Kudos for broodjepoep, by the way. :-)

顺便说一下,broodjepoep获得了荣誉。:-)

#2


1  

Solution to the issue so far:

到目前为止问题的解决办法:

var enc = Encoding.GetEncoding(0);

byte[] buffer = enc.GetBytes(text);
var sha1 = SHA1.Create();
var hash = BitConverter.ToString(sha1.ComputeHash(buffer)).Replace("-","");
return hash;

#1


3  

I'm not sure how 'SHA-1 Online' represents your hash, buts since it's a hash, it can contain characters that can't be represented in a (UTF8) string. I think you're better off using Convert.ToBase64String() to easily represent the byte-array hash in a string:

我不确定“SHA-1在线”是如何表示你的哈希,但由于它是一个散列,它可以包含不能用(UTF8)字符串表示的字符。我认为您最好使用Convert.ToBase64String()来轻松地表示字符串中的字节数组散列:

var hashString = Convert.ToBase64String(hash);

To convert it back to a byte-array, use Convert.FromBase64String():

要将它转换回字节数组,请使用convert . frombase64string ():

var bytes =  Convert.FromBase64String(hashString);

Also see: Converting a md5 hash byte array to a string. Which shows there a multiple ways to represent hash in a string. For example, hash.ToString("X") will use a hexadecimal representation.

参见:将md5散列字节数组转换为字符串。它显示了在字符串中表示哈希的多种方式。例如,hash.ToString(“X”)将使用十六进制表示。

Kudos for broodjepoep, by the way. :-)

顺便说一下,broodjepoep获得了荣誉。:-)

#2


1  

Solution to the issue so far:

到目前为止问题的解决办法:

var enc = Encoding.GetEncoding(0);

byte[] buffer = enc.GetBytes(text);
var sha1 = SHA1.Create();
var hash = BitConverter.ToString(sha1.ComputeHash(buffer)).Replace("-","");
return hash;