如何将数组中的项目转换为字节以进行哈希处理?

问题描述 投票:0回答:2

我正在尝试对通过easygui输入的用户输入进行哈希处理。 Easygui将输入存储到数组中(我认为),因此当我尝试对用户输入进行哈希处理时,我不确定如何将其转换为字节。

这是我的代码:

import hashlib
import easygui


g = hashlib.sha256(b'helloworld').hexdigest()

l = easygui.enterbox('enter password')

f = hashlib.sha256([l]).hexdigest()

print(g)
print(f)

理想情况下,如果我在easygui中键入'helloworld',它应该返回相同的哈希输出。

当前错误是:

"TypeError: object supporting the buffer API required" at the line f = haslib.sha256([l]).hexdigest()
python python-3.x hash hashlib
2个回答
2
投票

easygui.enterbox返回用户输入的文本,如果取消操作,则返回None。您将不得不将返回的文本转换为字节数组。 Docs

if l is not None:
    f = hashlib.sha256(l.encode()).hexdigest()

0
投票

您必须对给定的字符串进行编码,然后才能对其进行哈希处理。最简单的方法是仅使用为字符串实现的encode()方法。

f = hashlib.sha256(l.encode()).hexdigest() print(f)

并返回您的sha256哈希。

© www.soinside.com 2019 - 2024. All rights reserved.