pysha3 not giving the right answer

2k views Asked by At

With the following code,

# $ pip install pysha3
import sys
if sys.version_info < (3, 4):
    import sha3

import hashlib

s = hashlib.new("sha3_512")

s.update(b"")
print(s.hexdigest())

I am getting

0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e

instead of

a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26

cf. https://en.wikipedia.org/wiki/SHA-3#Examples_of_SHA-3_variants

Could anyone advise me?

2

There are 2 answers

3
Martijn Pieters On BEST ANSWER

The pysha3 module you found was based on an draft of the SHA-3 specification, before it was standardised.

The module was created as a POC for Python issue 16113, and the code has not been updated since 2012. The NIST standard wasn't finalised until October 2015. As such, the implementation can't be used if you expect it to follow the released standard.

That ticket links to an implementation that does claim to have been updated to the standard: https://github.com/bjornedstrom/python-sha3. That package doesn't appear to be listed on PyPI, but can be installed with pip directly from GitHub:

pip install git+https://github.com/bjornedstrom/python-sha3

and this package does produce the expected result:

>>> import hashlib
>>> import sha3
>>> hashlib.sha3_512(b'').hexdigest()
b'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'

This package doesn't patch the built-in hashlib.new() constructor, but that's easily done by plugging in the constructor into the module cache:

>>> hashlib.__builtin_constructor_cache['sha3_512'] = sha3.sha3_512
>>> hashlib.new('sha3_512')
<sha3.SHA3512 object at 0x10b381a90>
0
quasoft On

SHA3 has been added to the built-in hashlib module in Python 3.6:

What’s New In Python 3.6

The SHA-3 hash functions sha3_224(), sha3_256(), sha3_384(), sha3_512(), and SHAKE hash functions shake_128() and shake_256() were added. (Contributed by Christian Heimes in issue 16113. Keccak Code Package by Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van Assche, and Ronny Van Keer.)

It can be used in the follow way:

>>> import sys
>>> import hashlib
>>> s = hashlib.new("sha3_512")    # sha3_224, sha3_256 and sha3_384 are also available
>>> s.update(b"")
>>> print(s.hexdigest())
a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26