加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在python实现中的Sha-3

发布时间:2020-12-20 12:01:54 所属栏目:Python 来源:网络整理
导读:我试图在 python中实现sha-3.下面给出的代码是我如何实现它.但我一次又一次得到以下错误. import sys import hashlibarg1 = sys.argv[1]with open(arg1,'r') as myfile: data=myfile.read().replace('n','')import sha3s=hashlib.sha3_228(data.encode('utf
我试图在 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读