在python实现中的Sha-3

我试图在
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库中不存在的函数.您想从包含pysha3的模块sha3调用函数sha3_228.实际上,sha3_228不存在,存在sha3_224.

只需用sha3.sha3_224替换hashlib.sha3_228即可.

并确保已使用命令安装了pysha3

python -m pip install pysha3

这是一个例子

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