[Ethereum] Generate ethereum address in C#

addresseskeccak

I'm following the instructions here to generate a valid ethereum address from scratch. I'm using BouncyCastle's secp256k1 to do the privkey –> pubkey, but I don't know where to get a proper implementation of keccak for C# to do the final step pubkey –> address. Can anyone help me with this?

The BouncyCastle KeccakDigest does not return the same result as in the instructions for the same pubkey.

Best Answer

BouncyCastle KeccakDigest does seem to work : https://github.com/Nethereum/Nethereum/blob/master/src/Nethereum.Util/Sha3Keccack.cs

public byte[] CalculateHash(byte[] value)
        {
            var digest = new KeccakDigest(256);
            var output = new byte[digest.GetDigestSize()];
            digest.BlockUpdate(value, 0, value.Length);
            digest.DoFinal(output, 0);
            return output;
}

The complete process of generating an address can be understood by the code in https://github.com/Nethereum/Nethereum/blob/master/src/Nethereum.Signer/EthECKey.cs, especially

public string GetPublicAddress()
{
            var initaddr = new Sha3Keccack().CalculateHash(GetPubKeyNoPrefix());
            var addr = new byte[initaddr.Length - 12];
            Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
            return new AddressUtil().ConvertToChecksumAddress(addr.ToHex());
}
Related Topic