我正在尝试创建一个名为checksum.dat的文件,其中包含Python中名为index.txt的文件的SHA256哈希。如何从Python中的.txt文件生成checksum.dat哈希文件
我想出迄今:
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版本是你的工作吗? – 2014-10-18 13:38:16
我关于Python 3.4.2 – LoLei 2014-10-18 14:22:01