0

I have a contract that stores sha-256 hashes of documents (stored as bytes32). I also have a method to retrieve a hash that is mapped to a unique id for the document. However, when I retrieve and compare the hash from the contract, it isn't the same as the hash of the document I sent. The contract is very simple - literally just a getter and a setter - so I'm not manipulating the hash before storing it in any way. Is this a known thing with either the EVM or the web3j library? I can always store the hex string representation of the hash instead of the byte array, but it would still be good to know why this is happening.

0

I have notice the same problem when using bytes32 and solidity sha. It was ok when using string type. You have to use a modified sha lib like this : https://github.com/raineorshine/solidity-sha3 to match the solidity sha each time.

0

There is a historical explanation for this. Suffice it to say that there is a procession of algorithm versions in the world at large, in the Solidity compiler's implementations functions and in client-side libraries. It's not going to work unless all parties on all sides are using the same algorithm.

You can have a look at this for more insight: Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?

This is the sort of problem we can expect in the future, given that all clients in any language will always need to match the algorithm used by the contract - whatever you decide to use today.

How can one help ensure that it will be possible for any client to match what the contract does, and always will be?

A solution to that problem is often to expose a pure function to help. pure, so a client can use it without transmitting sensitive information to the blockchain. (Caveat, if using Infura then it would be sent to their nodes).

function hashHelper(bytes memory data, args, etc) public pure returns(bytes32) {
  return keccak256(abi.encodePacked(data, args, etc));
}

Use that internally and expose externally so it is convenient for a client to match the contract's process.

Hope it 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.