Create hash in c# and validate in solidity

by fancycat   Last Updated January 14, 2018 00:28 AM

I'm trying to build an app that will call a contract from javascript. I want the contract to validate that the parameters weren't tampered with. In order to do that, my plan was to figure out the parameters server-side, hash the combination of parameters, and sign the hash. I'd pass the hash and signature along to the contract for the contract to validate. The hash would prove the parameters haven't changed and the signature would prove that I generated the hash.

I'm currently trying to figure out the hashing part.

void Main()
{
    var bytes = ASCIIEncoding.ASCII.GetBytes("abc");

    SHA256 hasher = SHA256Cng.Create();
    var hashed = hasher.ComputeHash(bytes);
    Console.WriteLine(BitConverter.ToString(hashed).Replace("-", string.Empty));
}

gives me an output of BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

On the other side of the fence I have

pragma solidity 0.4.18;    

contract HashCheck {    
    function hash(bytes32 toHash) public returns(bytes32) {
        bytes32 hashed = sha256(toHash);
        return hashed;        
    }
}

which returns

truffle(development)> f.hash.call("abc")
'0x26426d7cb06a12643ccfe84107603083d835c37f000a12f734137a0c8df77f26'

I'm happy to try out keccak (sha3) hashing but I haven't figured out how to do that in C# yet.



Related Questions



How to get two points (x, y) from an encoded pubkey?

Updated October 10, 2018 11:28 AM

Which cryptographic hash function does Ethereum use?

Updated September 27, 2017 21:28 PM


What is a block hash?

Updated December 13, 2017 04:28 AM