2

Is there a js function/library which gives same output as sha256 in solidity? I am saving a hash in a mapping and can access it via cli (truffle console) and see what the hash is but I would like to send this same hash as an email. So on the frontend I need to get same hash and send it over via POST request to backend which then handles the email.

2 Answers 2

3

EDIT

As pointed out in the comments, soliditySHA3 mimics Solidity's sha3/keccak256, functions, which are not related to SHA256.


web3.utils.soliditySHA3 does what you want: https://web3js.readthedocs.io/en/1.0/web3-utils.html#soliditysha3

2
  • 1
    As I know, there's a big difference between sha256 and sha3, which is actually a Keccak-256 hash function. Sep 26, 2019 at 11:44
  • 1
    You're 100% right. I have no idea why I answered this way or why the answer was accepted. :-) I'm going to edit the answer to make that clear.
    – user19510
    Sep 26, 2019 at 12:07
0

I ran into a huge problem trying to do that. I've tried everything and didn't find yet a proper solution to generate a hash in nodejs that is the same from a hash generated in solidity sha256 function. However, I came across this "possible" (I will explain why in quotes later) solution:

    function HashUtility(bytes32 val) public pure returns (bytes32) {
        bytes32 h = sha256(abi.encodePacked(val));
        return h;
    }

Instead of trying to generate the same hash you could use this function above in solidity to generate an equal hash from a value in nodeJs. Then, in your front-end or in truffle you would do something like this:

const secret = web3.utils.asciiToHex('A');
const result = await instance.HashUtility.call(secret);

In the code above I'm converting the string 'A' to hexadecimal first, which gives me 0x41 then calling the method HashUtility to hash it. The resulting hash is:

0x037371bf62c8a2c23f8134fc2c7049fbe698860b888c98b955644c4b8f8f3155.

Which I couldn't find any other sha256 function that made the same hash. Hashing with nodejs crypto sha256 doesn't give the same hash in any way I tried. Maybe it is because the hash is returned in hexadecimal value and I have to convert the generated string to hexadecimal? tried that, but didn't work.

I also have to say that I'm not quite sure that using the solidity HashUtility function I made above is a good solution. I'm not sure, but miners can see the params you send to the function and that could be a problem, because you would be revealing a value that you don't want to. Which means that the best solution would be a way to hash it BEFORE sending it to the contract. I don't know why it is so painful to do it in nodeJS. In python with brownie it worked very easy. I'm still looking for a solution though, might find it soon.

2
  • The web3 pads 'A' = 0x41 to the right with 0 bytes to bytes32, something like sha256(Buffer.from('4100000000000000000000000000000000000000000000000000000000000000', 'hex')).
    – Ismael
    Sep 28, 2021 at 4:17
  • Yeah, that's right. basically just add padding to the right. I found it out and left an example of it in this other question: ethereum.stackexchange.com/questions/110614/… Sep 28, 2021 at 17:09

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.