如何在C#中获取sha3-512哈希?

7
"test"的字符串在https://md5calc.com/hash/sha3-512/test中会得到"9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7b710110c0a678288166e7fbe796883a4f2e9b3ca9f484f521d0ce464345cc1aec96779149c14"这个值。
using HashLib; // from https://www.nuget.org/packages/HashLib
using System;

class Program
{
    static void Main(string[] args)
    {
        var sha3512 = HashFactory.Crypto.SHA3.CreateKeccak512();
        var tempHash = sha3512.ComputeString("test");
        Console.WriteLine(tempHash.ToString().ToLower().Replace("-", ""));
    }
}

返回值为 "3abff7b1f2042ac3861bd4c0b40efaa8a695909bfb31753fc7b1bd69778de1225a627c8f07cf4814cc05435ada2a1ffee3f4513a8867154274787624f24e551b"

我需要获取第一个字符串,而不是hashlib提供的内容。


2
哪一个是错的?你确定这一行不是问题所在吗 Console.WriteLine(tempHash.ToString().ToLower().Replace("-", "")); - Jodrell
@Jodrell 网站上的 9ece 是正确的。 - Jon
1
这个网站使用的是utf8编码吗?我猜测.net的那个可能在使用不同的编码方式。 - Daniel A. White
什么是HashFactory - Jodrell
1
请参考以下链接:https://dev59.com/6IDba4cB1Zd3GeqPEG1f - Jeff
2
请注意,Keccak512与SHA-3不同 - 它不会像实际的512位SHA-3实现一样给出相同的哈希值。 - Jon
1个回答

17

您可以使用BouncyCastle。它有一个SHA3实现。

var hashAlgorithm = new Org.BouncyCastle.Crypto.Digests.Sha3Digest(512);

// Choose correct encoding based on your usecase
byte[] input = Encoding.ASCII.GetBytes("test");

hashAlgorithm.BlockUpdate(input, 0, input.Length);

byte[] result = new byte[64]; // 512 / 8 = 64
hashAlgorithm.DoFinal(result, 0);

string hashString = BitConverter.ToString(result);
hashString = hashString.Replace("-", "").ToLowerInvariant();

Console.WriteLine(hashString);

输出为

9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7b710110c0a678288166e7fbe796883a4f2e9b3ca9f484f521d0ce464345cc1aec96779149c14


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接