pythonsha-3keccak

Sha-3 in python implementation


I am trying to implement sha-3 in python.The code given below is how I implemented it.But i am getting the below error again and again.

import sys 
import hashlib
arg1 = sys.argv[1]
with open(arg1, 'r') as myfile:
     data=myfile.read().replace('\n', '')
import sha3
s=hashlib.sha3_228(data.encode('utf-8')).hexdigest()
print(s)

The following error is what i get when I execute it.

Traceback (most recent call last):
File "sha3.py", line 6, in <module>
import sha3
File "/home/hello/Documents/SHA-3/sha3.py", line 7, in <module>
s=hashlib.sha3_228(data.encode('utf-8')).hexdigest()
AttributeError: 'module' object has no attribute 'sha3_228'

The below link can be used for reference. https://pypi.python.org/pypi/pysha3


Solution

  • There are two problems here: one from your code, and one from the documentation, that contains a typo on the function you would like to use.

    You are calling a function that is not present in hashlib library. You want to call function sha3_228 from module sha3, that is shipped with package pysha3. In fact, sha3_228 does not exist, it is sha3_224 that exists.

    Simply replace hashlib.sha3_228 with sha3.sha3_224.

    And make sure you have installed pysha3, with command

    python -m pip install pysha3
    

    Here is an example

    import sha3
    data='maydata'
    s=sha3.sha3_224(data.encode('utf-8')).hexdigest()
    print(s)
    # 20faf4bf0bbb9ca9b3a47282afe713ba53c9e243bc8bdf1d670671cb