Solidity SHA3 – Why Aren’t Solidity SHA3 Hashes Matching Other SHA3 Libraries?

cryptographykeccaksha3solidityweb3js

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?

Best Answer

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
Related Topic