0

I am realized that default hashlib.sha3_256 hasher does not calculates hashes like other solution, for instance, other python's modules. Below, for instance, I comparing hashlib and sha3 implementation of sha2_256 algorithm on Python 3.6.3.

Implemetation from sha3 gaves correct result (according to other internet resources) while hashlib.sha3_256 result is completely different. How it could be? Am I missed something?

import sha3
import hashlib

def test(s):
    k = sha3.keccak_256()
    k.update(s.encode())
    print('test string="{s}", sha3.keccak_256()="{h}"'.format(s=s, h=k.hexdigest()))

    h = hashlib.sha3_256()
    h.update(s.encode())
    print('test string="{s}", hashlib.keccak_256()="{h}"'.format(s=s, h=h.hexdigest()))


test('')
test('eth')
test('foo.bar')

Results:

test string="", sha3.keccak_256()="c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
test string="", hashlib.keccak_256()="a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
test string="eth", sha3.keccak_256()="4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0"
test string="eth", hashlib.keccak_256()="4b3cdfda85c576e43c848d43fdf8e901d8d02553fec8ee56289d10b8dc47d997"
test string="foo.bar", sha3.keccak_256()="461e2b648c9a6c0c3e2cab45884ae0fcab21c655fcf588f2a45e6596e3e0e9a7"
test string="foo.bar", hashlib.keccak_256()="e55dea66e750540f599874a18596745b0c5705bc6873ca3ef1ccd2acbba88670"
Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
  • I'm not a specialist, but as Sraw said, it's sure that they don't have the same implementation. here is a quote from Wikipedia on SHA-3 `For SHA-3-224, SHA-3-256, SHA-3-384, and SHA-3-512 instances, r is greater than d, so there is no need for additional block permutations in the squeezing phase; the leading d bits of the state are the desired hash. However, SHAKE-128 and SHAKE-256 allow an arbitrary output length, which is useful in applications such as optimal asymmetric encryption padding.` – Curcuma_ Apr 12 '18 at 10:14

1 Answers1

0

I think they are different implementations:

>>> hashlib.sha3_256("asdf".encode()).hexdigest()
'dd2781f4c51bccdbe23e4d398b8a82261f585c278dbb4b84989fea70e76723a9'
>>> sha3.sha3_256("asdf".encode()).hexdigest()
'dd2781f4c51bccdbe23e4d398b8a82261f585c278dbb4b84989fea70e76723a9'
>>> sha3.keccak_256("asdf".encode()).hexdigest()
'4c8f18581c0167eb90a761b4a304e009b924f03b619a0c0e8ea3adfce20aee64'

After a search, I find that's true.

https://github.com/status-im/nim-keccak-tiny/issues/1

edit

After further search... It seems keccak_256 is not a standard implementation.

Sraw
  • 18,892
  • 11
  • 54
  • 87