I have a Solidity 0.4.26 smart contract. I'm using a commit-reveal pattern to hide to first user's commitment move.

I have a helper contract Hasher that hashes the user's commitment with a hash() function:

contract Hasher{
    /** @dev Give the commitement. Must only be called locally.
     *  @param _c The move.
     *  @param _salt The salt to increase entropy.
    function hash(uint8 _c, uint256 _salt) constant returns(bytes32) {
        return keccak256(_c,_salt);

The Solidity 0.4.26 implementation of a native keccak256 function allows 2 arguments: a message and a salt phrase.

For some reason, I cannot find an equivalent of that function in any JS-based library.

I'm ideally looking to code a Javascript keccak256 function that would be compatible with the Solidity's 0.4.26 keccak256 in order to later decrypt the commitment move with the salt:

require(keccak256(_c1,_salt)==c1Hash); // Verify the value is the commited one.

Please give me any advice on how I can achieve that.

Thank you!

1 Answer 1


To do the equivalent hashing in JavaScript, you have to encodePacked the values _c and _salt before hashing. In ethers there is a helper method solidityKeccak256 which does exactly what you are looking for.

const _c = 5;
const _salt = 10;
const hash = ethers.utils.solidityKeccak256(['uint8', 'uint256'], [_c, _salt]);
  • Works like a charm! Thanks!
    – Mila A
    Commented Jun 27, 2023 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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