我正在嘗試創建一個名爲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