27

Solidity has a function named sha3 and I tested it with an empty string input. It is not matching what other sha3 libraries produce. I see contracts written by others using the same sha3 function in Solidity, and their hashes also do not match. The same goes for Javascript web3.sha3 hashes. Why?

5
  • 2
    Since you are self answering this; could you give some code samples showing the difference in output and in the answer how to configure such a sha3 library for Keccak-256 compatibility?
    – J-B
    Jan 24, 2016 at 21:06
  • 1
    added samples to answer
    – eth
    Jan 24, 2016 at 22:59
  • 4
    Possible duplicate of Which cryptographic hash function does Ethereum use? Jan 25, 2016 at 1:41
  • 2
    People coming from a search engine are going to use different terms. This is likely to be the type of problem/question people are going to ask.
    – eth
    Jan 25, 2016 at 9:23
  • 1
    @eth As someone visiting from a search engine I can verify that your assumption was correct, I am glad this was not merged for a duplicate with less optimal SEO. May 8, 2020 at 22:22

2 Answers 2

26

Ethereum uses Keccak-256, instead of the SHA-3 FIPS 202 standard. In the sha3 libraries you are using, try looking for the option to specify using Keccak-256.

For Python see Getting Method ID "Keccak hash" in Python

For Javascript, this library js-sha3 would involve using the keccak_256 function instead of sha3_256.

keccak_256('');
// c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

sha3_256('');
// a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a

Others:

keccak_256('The quick brown fox jumps over the lazy dog');
// 4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15

keccak_256('The quick brown fox jumps over the lazy dog.');
// 578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d

sha3_256('The quick brown fox jumps over the lazy dog');
// 69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04

sha3_256('The quick brown fox jumps over the lazy dog.');
// a80f839cd4f83f6c3dafc87feae470045e4eb0d366397d5c6ce34ba1739f734d
4
  • 3
    As seen in js-sha3 the only difference is the padding, 0x06 for FIPS202, 0x01 for Keccak-256.
    – user16111
    Jul 26, 2017 at 9:32
  • I'm not sure this comment is accurate: (1) keccak-256 is not a thing (2) see @axic comment bellow for a more correct answer. Sep 27, 2017 at 11:04
  • keccak() can allow multiple inputs inside the smart contract as: keccak256(97, 98, 99). How could we achieve this using keccak_256() that you represented? @eth♦
    – alper
    Dec 26, 2017 at 13:34
  • 1
    @Alper If you've seen the linked and related questions, I think posting a new and well-written question would be better. (The answer for your example is to use "abc" but a general answer is better.)
    – eth
    Dec 30, 2017 at 14:08
18

Extending on this answer:

Ethereum uses Keccak hashing, which was submitted to the NIST competition for SHA3. It was subsequently selected as a winner in 2012, but the final standard was only released in August 2015.

Ethereum uses Keccak in its form as it was selected in 2012. Too bad some parameters of the algorithm were tweaked for the final specification in 2015 making it incompatible with the original.

Most tools online implement the final 2015 version of SHA3.

2
  • I'm upvoting but fyi I linked to ethereum.stackexchange.com/questions/550/… instead of "rehashing" the NIST stuff :)
    – eth
    Mar 11, 2016 at 18:58
  • I've totally missed that is a link. I think based on these three-four answers it would make sense to make them into a single wiki page.
    – axic
    Mar 11, 2016 at 19:04

Your Answer

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

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