in both geth and web3.js, when we call

> web3.sha3("0x536f6d6520415343494920737472696e6720746f20626520686173686564")

we get:

requirement of using web3.fromAscii before sha3 is deprecated
new usage: 'web3.sha3("hello")'    
see https://github.com/ethereum/web3.js/pull/205
if you need to hash hex value, you can do 'sha3("0xfff", true)'

So I proceed to do this:

> web3.sha3("0x536f6d6520415343494920737472696e6720746f20626520686173686564",true)

upon which I get:


This does not match the original value that I intended to get, namely


I was wondering if someone could explain to me why the two values are different. The reason I'm asking is that I'd like to sha3 this:


which gives me a invalid byte index error, but I know that


is not going to get me what I want, based on the above observations.

  • When you say the value you "intended to get", what does that mean? The Ethereum sha3 is actually not actually sha3, but the original Keccak. See ethereum.stackexchange.com/questions/559/… Jan 31, 2016 at 1:33
  • Thanks for your comment Tjaden. By "intended to get" I mean the value at the the of the error message, which is also the same value that we get from a contract running the sha3 code. Someone kindly pointed out that my software version was out of date, which was causing my issues.
    – shiso
    Feb 1, 2016 at 14:14

2 Answers 2


Please use a newer version of Geth and web3.js, because the error message you displayed indicates they are old: the error has been removed.

The API is to use {encoding: 'hex'} as the 2nd argument. (true is not supported anymore and ignored.)

With a newer version, you will get the Keccak-256 hash you are expecting:

> web3.sha3("0x536f6d6520415343494920737472696e6720746f20626520686173686564",{encoding:'hex'})

For 0xfff:

> web3.sha3('0xfff', {encoding:'hex'})

Here is an updated version of Geth which behaves as described above:

> web3.version
  api: "0.15.1",
  • This was used in the old version of web3. It might return web3.sha3 is not a function in the current version. Using web3.utils.sha3.
    – Ender
    Sep 10, 2019 at 4:10
  • 1
    @Ender Thank you. Yes, web3.js will keep getting updated, as of this moment there is 1.x and also a branch for 2.x...
    – eth
    Sep 21, 2019 at 2:44

You get different output because you're asking web3.sha3 to interpret your input differently when you pass in true.


Without the true, web3.sha3 runs toAscii on your input before running SHA3 on it. Basically, it tries interpreting the hex pairs in your hex string into ASCII characters and then runs SHA3 on the resulting string. This is also why you get that error when trying to run web3.sha3('0xfff'): the hex string isn't composed of hex pairs and so can't be interpreted as representative of an ASCII string.


With the true, it runs SHA3 on your string as-is. Whatever you pass in is exactly what gets passed on to CryptoJS's SHA3 function.

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.