15

I am using Python 2.7 and need to find keccak hash for solidity events. However I don't see a default lib for the same.

I installed sha3 but it doesn't seem to provide this functionality. Tried pysha3 with below code

  import sha3
  k = sha3.keccak_512()
  k.update('age')
  k.hexdigest()

But got the error

AttributeError: 'module' object has no attribute 'keccak_512'

sha3 doesn't have this module indeed

>>> dir(sha3)
['SHA3224', 'SHA3256', 'SHA3384', 'SHA3512', 'SHAKE128', 'SHAKE256', '_SHA3Base', '_SHAKEBase', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_sha3', 'binascii', 'copy', 'hashlib', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'shake128', 'shake256']
garg10may
  • 5,794
  • 11
  • 50
  • 91
  • Did you even check the official website of **Keccak** (spelling!)? – sascha Sep 18 '17 at 12:23
  • was a typo due to touch type. – garg10may Sep 18 '17 at 12:24
  • Did you install a native sha3 library and expect it to just work? If so, please take a look at the python wrapper [pysha3](https://pypi.python.org/pypi/pysha3). – KompjoeFriek Sep 18 '17 at 12:25
  • installed `pysha3` but it doesn't seem to work. Gives error `AttributeError: 'module' object has no attribute 'keccak_512' ` – garg10may Sep 18 '17 at 12:29
  • @KompjoeFriek I am naive in cyrptography, python 3.6 suggest to have something related to `keccak`. I was looking just looking for something pythonic. – garg10may Sep 18 '17 at 12:33
  • Can you please update your question with the relevant python code including the imports, a mention of the pysha3 wrapper and the error you got. – KompjoeFriek Sep 18 '17 at 12:35
  • why a downvote? – garg10may Sep 18 '17 at 12:58
  • Can you please also include the output of `dir(sha3)` to your question. It looks like you ran into this issue: https://github.com/tiran/pysha3/issues/8. However, it does not mention which python version is used there. Your code does work python 3.6.x (only difference is it requires a byte literal for `update()`, just like all of hashlib does). – KompjoeFriek Sep 18 '17 at 13:21
  • 1
    `pysha3` is working, `sha3` was overriding it. Original implementer should give a different import name. – garg10may Sep 18 '17 at 13:34

1 Answers1

34
  1. pycryptodome pip install pycryptodome

     from Crypto.Hash import keccak
     k = keccak.new(digest_bits=256)
     k.update('age')
     print k.hexdigest()
    
  2. pysha3 pip install pysha3

    import sha3
    k = sha3.keccak_256()
    k.update('age')
    print k.hexdigest()
    

Please note:

  1. For python3 the strings need to be passed in binary k.update(b'age')
  2. If you want to find a new hash you need to initialize k again otherwise it will update the value on top of the existing one.

If you have both sha3 and pysha3, it won't work, since import sha3 will default to sha3 lib.

Online handy tool

garg10may
  • 5,794
  • 11
  • 50
  • 91
  • The "online handy tool" and the python code return separate hashes. For `k.update("0") print (k.hexdigest())` it returns the same than the website `044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116` but for "1" it's different. Why does it in the one case accept the input as a number but in the other case seems to take the ""? – Senju Jun 29 '18 at 10:39
  • 6
    For the ppl trying to use this for ethereum: hashlib does not return the correct hashes, pycryptodome is the one you want! – reijin Nov 26 '18 at 17:51
  • @garg10may and for a compatible pypy answer? – user2284570 Oct 27 '19 at 01:39
  • FYI @garg10may pycryptodome example needs to be passed a binary string (i.e. `b'age'`) instead of a string – lsankar4033 Oct 11 '20 at 17:32
  • This is because you are using `python3`. For `python2` it will work. I have now highlighted the same in answer. – garg10may Oct 13 '20 at 10:48
  • @Senju sorry for replying after a year, but you need to perform the sha3.keccak_256() step again for a new hash value, I am assuming you did k.update("1") step after finding the hash of 0 so essentially you were finding hash of 01 hence they were not matching. You can match them with 01 on the tool they should match – garg10may Oct 13 '20 at 10:48
  • ModuleNotFoundError: No module named 'sha3' after pip install sha3 – rsc05 Jul 23 '22 at 17:45
  • pls use `pip install pysha3` – garg10may Jul 26 '22 at 07:03