Simple implementation of SHA-3 Keccak hashing to the wrong output in C#?

寵の児 提交于 2019-12-01 11:29:12

问题


I'll keep it short and simple! I'm learning C#, and I am trying to get the HashLib library @ https://hashlib.codeplex.com/ working for the new SHA-3 Keccak algorithm. I've written a simple Console/Win32 application that supposedly has to output the correct hash code, but it doesn't!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using HashLib;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                string passPhrase = "";
                IHash hash = HashFactory.Crypto.SHA3.CreateKeccak512();
                HashResult r = hash.ComputeString(passPhrase, System.Text.Encoding.ASCII);

                Console.WriteLine(r.ToString().ToLower().Replace("-",""));
                Console.WriteLine("{0}, {1}, {2}", hash.BlockSize, hash.HashSize, hash.Name);
                Console.ReadLine();
            }
        }
    }
}

The application builds and runs ok, but the output is very wrong. When I use other people 's implementations of the Keccak algorithm, I get different results and it doesn't match for example this wiki post either. https://en.wikipedia.org/wiki/SHA-3 So something is obviously wrong.

When I leave the text empty, as per example, I get the following: "df987cfd23fbc92e7e87faaca300ec3f etc. etc." while the wiki and other tools say I should get

"0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e"

,which is something entirely different. I also tried it with non-empty strings of course.

Does anyone have a suggestion?


回答1:


Your version of HashLib is too old. If you look at the recent changes you can see the test vector changed from the one you got to the one you should get. (The algorithm changed as well, of course.)



来源:https://stackoverflow.com/questions/14061299/simple-implementation-of-sha-3-keccak-hashing-to-the-wrong-output-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!