0

If I am using web3py to hash some data, like so,

msgHash = w3.soliditySha3(['bytes32','bytes32'], [dataBytes, saltBytes]);

What would be the correct way to hash inside of a Solidity contract and expect the same resultant hash?

In Solidity (v0.4.24) I have tried,

keccak256(abi.encodePacked(data, salt))

sha3(data, salt)

and a few other ways but no luck. I believe there is a specific way to do this and the exact hashing algorithms don't always match. In Solidity the data type I am using is,

bytes32

Could the text= prefix be messing this up? Also is there a specific prefix that Solidity uses?

dataBytes = w3.toBytes(text=data);
saltBytes = w3.toBytes(text=salt);
1

The issue is how you manage the text. You can do:

data = 'some data';
salt = 'some salt data';      
msgHash = w3.soliditySha3(['bytes32','bytes32'], [bytes(data.encode()), bytes(salt.encode())]);

In solidity:

string memory data = 'some data';
string memory salt = 'some salt data';
hash = keccak256(abi.encodePacked(data,salt));

This will produce the same hash.

If you have for instance int256 there should be no problem:

data = 'some data';
salt = 1234567;      
msgHash = w3.soliditySha3(['bytes32','int256'], [bytes(data.encode()), salt]);

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.