【问题标题】:How can i get sha3-512 hash in C#? [duplicate]如何在 C# 中获取 sha3-512 哈希? [复制]
【发布时间】:2019-09-09 15:50:16
【问题描述】:

例如,https://md5calc.com/hash/sha3-512/test 中的字符串“test”给了我“9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7b710110c0a678288166e7fbe796883a4f2e9b3ca9f484f521d0ce4761496”

但是

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 提供的。

【问题讨论】:

  • 嗯,哪一个是错的?你确定这条线不是你的问题Console.WriteLine(tempHash.ToString().ToLower().Replace("-", ""));
  • @Jodrell 网站上的 9ece 是正确的
  • 网站是在做utf8的吗?我猜 .net 正在做一些不同的事情。
  • 请注意,Keccak512 与 SHA-3 不同 - 它不会提供与实际 512 位 SHA-3 实现相同的哈希值。

标签: c# hash sha hashlib sha-3


【解决方案1】:

您可以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2020-01-27
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多