How to hash a number, and check the result in a solidity contract (probably data type confusion)

edmundedgaredmundedgar Member Posts: 19 ✭✭
I'm trying to make a hash of a number (in python) then have a solidity contract recreate the hash, then do something useful with the number.
(The motivation for this is that I ultimately want to sign the data and pass in the signature, then check it using ecrecover to validate the data, which is all working fine apart from the hashing part.)

It's working fine for me if I hash a string, and pass the string into the contract. But then I'm stuck with a string in my contract, and I don't know how to change that into an int or do anything useful with it.

If I pass the contract bytes32 or int256 and call sha3() on that it produces some kind of hash, but I can't work out exactly what it's hashing, or how I would create the same hash myself in Python.

Can anyone tell me how to do any of:
a) Recreate the hash I'm getting in the contract outside the contract
b) Pass in the data to the contract in such a way that it creates the hash I'm making outside the contract
c) Change the string that I'm successfully hashing right now into bytes32 or int256 inside the contract?


  • edmundedgaredmundedgar Member Posts: 19 ✭✭
    edited October 2015
    Ah, got the int hashing working, thanks to some code someone much smarter than me wrote who was working on Reality Keys integration.

    In case anyone else is wondering, you make the hash outside the contract like this to match what the contract is doing:
    sha3_256(decode_hex(format(mynum, 'x').zfill(64))).digest()

    * Start with a number
    * Hex-encode-it
    * Pad the left side with zeros to make up to 64 hex characters (as it's going to go into 32 bytes)
    * Decode the hex again, and you're ready to hash.
  • chrisethchriseth Member Posts: 170 ✭✭✭
    Are there still any open questions?
    Solidity's sha3 is applied to its arguments without padding. This means that sha3(uint8(2)) should be equal to


    string s = "abc";


  • edmundedgaredmundedgar Member Posts: 19 ✭✭
    Thanks, Chris, got it.
Sign In or Register to comment.