4

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

1
  • You are trying to use it, not implement it. Implementing means to write code that does the actual hash calculation. That's not what you're doing here.
    – Bachsau
    Commented Jan 29, 2021 at 21:28

3 Answers 3

11

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
1
  • Small note here: Sha3 is supported in Python 3.6 (as well as a few other added algorithms)
    – Daniel B.
    Commented Jan 24, 2018 at 15:30
2

I had the same problem. I installed sha3 by itself first. That doesn't work. Then I installed pysha3 and it still didn't work. I finally uninstalled both sha3 and pysha3. Then I reinstalled just pysha3 and it worked fine.

-1

You likely need to include:

import sys

if sys.version_info < (3, 6):
    import sha3

This is because is lower versions of python sha3 aren't included by default in hashlib.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.