1

I want to pass a Keccak-256 hashed value from the front-end, say a string "experiment". I store the value in Solidity, and, when passing the same value for the second time, a Solidity function needs to compare the values, and give true, if those values are identical.

I have been unsuccessful of comparing the values so far. I am trying this online tool: Link , and then generate the value in Solidity via keccak256('experiment'). The results do not seem to match.

Any advice on how to achieve that?

  • 1
    For me, the results of hashing "experiment" with that tool and also with that line of code in Solidity are both 0x047dbe311ff69923d959629005cb27fb9d3c725f890180fb93365b03a25f0a58. – user19510 May 18 '18 at 3:52
  • That is utterly strange, as I'm not getting the similar result... Thanks, I will check the rest of the code. – Ruham May 18 '18 at 4:08
3

The two values should be identical (they were when I tested). However, the tool you linked to doesn't append the "0x" to the start of the hex value, whereas Solidity does. But they both represent the same hex value.

1

You could have used web3's soliditySha3 function to get the equivalent of solidity's keccak256, as follows:

const frontEndHashed = web3.utils.soliditySha3({ type: 'string', value: "experiment" });

// or just this:

const frontEndHashed = web3.utils.soliditySha3("experiment");

// And if you send that to solidity it would match the following.

string value = "experiment";
bytes32 solidityHashed = keccak256(abi.encodePacked(value));

Then you would have frontEndHashed == solidityHashed. In solidity keccak256 is just an alias for sha3.

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.