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
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
I have the nuget package System.Security.Cryptography.Algorithms
installed (v 4.3.0)
Also tried with GetEncoding(0)
to get the sys default coding. Also did not work.