python实现中的Sha-3

2024-05-23 13:28:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用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


Tags: pyimportdata错误syslinemyfileutf
2条回答

这里有两个问题:一个来自代码,另一个来自文档,其中包含您想要使用的函数的错误。

调用的函数不在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

我也有同样的问题。我先自己安装了sha3。那不管用。然后我安装了pysha3,它仍然没有工作。我终于卸载了sha3和pysha3。然后我重新安装了pysha3,它运行良好。

相关问题 更多 >