首页 > 解决方案 > 如何将数组中的项目转换为字节以进行散列?

问题描述

我正在尝试对通过 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()

标签: pythonpython-3.xhashhashlib

解决方案


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

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

推荐阅读