python - 如何将数组中的项目转换为字节以进行散列?
问题描述
我正在尝试对通过 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()
解决方案
easygui.enterbox
返回用户输入的文本,如果取消操作,则返回 None。您必须将返回的文本转换为字节数组。文档
if l is not None:
f = hashlib.sha256(l.encode()).hexdigest()
推荐阅读
- linq.js - 搜索文本如何使用 linq.js 包含单引号?
- java - 覆盖 Dlog4j.configuration Java 参数
- shopify - 如何在不使用任何应用程序的情况下在 shopify 中创建基于标签的集合过滤器
- nativescript - 如何使用modalStack在nativescript中的文本字段下方显示键盘?
- java - Flutter:NoSuchMethodError:在 null 上调用了 getter 'nom'。接收者; null 尝试调用:nom
- android - 在 transformation.map 之后运行挂起函数
- android - kotlin 协程流中的并发
- postgresql - 是否可以使用带有 npgsql 的 EF CORE 3.1 中的 UNACCENT 扩展为 postgresql 进行选择?
- java - 为什么即使将目录设置为只读,Files.isWritable() 方法也会为目录返回 true?
- javascript - JQuery:图像上的 Onload 事件不起作用