【发布时间】:2014-12-13 22:21:56
【问题描述】:
我正在尝试创建一个名为 checksum.dat 的文件,其中包含 Python 中名为 index.txt 的文件的 SHA256 哈希。
到目前为止我的想法:
import hashlib
with open("index.txt", "rb") as file3:
with open("checksum.dat", "wb") as file4:
file_checksum = hashlib.sha256()
file_checksum.update(file3)
file_checksum.digest()
print(file_checksum)
file4.write(file_checksum)
我希望它将哈希打印到控制台中,并将其写入 checksum.dat 文件。
但我得到的只是这个错误:
File "...", line 97, in main
file_checksum.update(file3)
TypeError: object supporting the buffer API required
到目前为止,我在 Google 上搜索到的是,据我了解,您不能仅从字节对象或其他东西中对字符串进行哈希处理。不知道如何将我的 index.txt 变成我可以使用的对象。
有人知道如何解决这个问题吗?请记住,我是新手。
【问题讨论】:
-
你正在使用哪个 Python 版本?
标签: python file text hash checksum