我正在尝试在 python 中实现 sha-3。下面给出的代码是我实现它的方式。但是我一次又一次地收到以下错误。
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)
下面的错误是我在执行它时得到的。
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'
以下链接可供引用。 https://pypi.python.org/pypi/pysha3
最佳答案
这里有两个问题:一个来自您的代码,另一个来自文档,其中包含您要使用的函数的拼写错误。
您正在调用 hashlib
库中不存在的函数。你想从模块 sha3
中调用函数 sha3_228
,它随包 pysha3
一起提供。事实上,sha3_228
并不存在,存在的是sha3_224
。
只需将 hashlib.sha3_228
替换为 sha3.sha3_224
。
并确保你已经安装了pysha3
,使用命令
python -m pip install pysha3
举个例子
import sha3
data='maydata'
s=sha3.sha3_224(data.encode('utf-8')).hexdigest()
print(s)
# 20faf4bf0bbb9ca9b3a47282afe713ba53c9e243bc8bdf1d670671cb
关于python - Python 实现中的 Sha-3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158351/