0

My Console App is usin .Net 6.0. I want to generate a crypto wallet KeyPair that corresponds to the public address. However when I hash it with Keccak 256, Using Nethereum Sha3Keccak as shown below, the hash is different than expected. The string that I want to hash is DF4566150FEE083F2A956B8666E666CD8AB6DC6A05A0D8722EDD8E175BD83FF662B0088FE94B03C5F1FD881B7CB522AE2AE0C19CC9BB4969EC702C7016C6D887

The result should be 53DB7117947F19F22349D28908B79CB67FF2E0D7A15D2721C49AB05B5287F315.
However, the result with my code is 7739b8b83fb1f0387e7ade80c6f77745ee105032c7333213feb2c5ed5e23f395.

I used an online utility - https://emn178.github.io/online-tools/keccak_256.html to try and figure it out and when I select HEX there it generates the proper hash, but when it's TEXT it provides 7739b8b83fb1f0387e7ade80c6f77745ee105032c7333213feb2c5ed5e23f395 as well.

Any suggestions as to what I'm doing wrong? Thank you.

using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Nethereum.Signer;
using Nethereum.Signer.Crypto;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Util;

namespace App1
{
 class Program
    {    
    static void Main ()
        {
            string str = "DF4566150FEE083F2A956B8666E666CD8AB6DC6A05A0D8722EDD8E175BD83FF662B0088FE94B03C5F1FD881B7CB522AE2AE0C19CC9BB4969EC702C7016C6D887"
            Sha3Keccack shaKec = new Sha3Keccack();
            byte[] ba = StringToByteArray(str);
            string hex = Convert.ToHexString(ba);
            string kec = shaKec.CalculateHash(hex);
            Console.WriteLine(kec);
        }
     
    public static byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
        }
    }
}
user3410566
  • 45
  • 2
  • 15
  • Can you explain the logic? The original data is a hex string. You convert it to byte[] and then again to hex snd then hash the hex value. Is the hash supposed to be calculated from binary data, hex uppercase or hex lowercase? – jps Sep 14 '22 at 09:43
  • That's how the address is generated. The mechanism is get 64bit hex private key, then ECDSA to get 130bit uncompressed address, then remove first 2 bits, then Keccak256 hash the remaining 128bits, then get last 40 chars, add 41 in front, Hash with SHA256 twice, get the first 8 bits as checksm, add at the end to the string with 41 infront, then base58 to get the public address. I will try the lower/uppercase HEX, thank you! – user3410566 Sep 14 '22 at 10:29
  • 1
    You get the desired result if you hex decode `str` (e.g. with `StringToByteArray()`) and hash this value. So the `Convert.ToHexString()` call is wrong. But it is more comfortable to apply `CalculateHashFromHex()`, which can *directly* process the hex encoded value: `string kec = shaKec.CalculateHashFromHex(str)`. This method returns the hash hex encoded, in lowercase (which of course can be converted to uppercase with `ToUpper()`). – Topaco Sep 14 '22 at 10:52

0 Answers0