DOTNETコア 内の文字列をハッシュするとき、私は奇妙な結果を得る:Computing SHA1 with ASP.NET Core をして、これは私のコードでどのようにconvert a byte array to string .NETコア使うSHA-1、.NETのコアで、私はこれと同様の問題を発見した
で発見しました:
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);
}
、これは私のテストで:
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));
出力:
MHnѐ&ȥGS
私は(V 4.3.0)nugetパッケージSystem.Security.Cryptography.Algorithms
がインストールされている。またGetEncoding(0)
にしてみました
sysのデフォルトのコーディングを取得します。また、動作しませんでした。
ビットオフトピックが、SHA-1は廃止されている(例えばhttps://blog.qualys.com/ssllabs/2014/を見ます09/09/sha1-deprecation-what-you-need-know-knowとhttps://blogs.windows.com/msedgedev/2016/04/29/sha1-deprecation-roadmapなど)を使用してください。強力な暗号が必要な場合は、SHA-1から離れるべきです。 – YSK
私は知っていますが、私が使用している外部サービスにそれを教えてください;-)(オランダの銀行) –