我正在编写一个简单的工具,它允许我快速检查下载的ISO文件的MD5哈希值。这是我的算法:
import sys
import hashlib
def main():
filename = sys.argv[1] # Takes the ISO 'file' as an argument in the command line
testFile = open(filename, "r") # Opens and reads the ISO 'file'
# Use hashlib here to find MD5 hash of the ISO 'file'. This is where I'm having problems
hashedMd5 = hashlib.md5(testFile).hexdigest()
realMd5 = input("Enter the valid MD5 hash: ") # Promt the user for the valid MD5 hash
if (realMd5 == hashedMd5): # Check if valid
print("GOOD!")
else:
print("BAD!!")
main()
当我尝试获取文件的MD5散列时,我的问题出现在第9行。我得到的类型错误:对象支持缓冲区API所需。有人能解释一下如何使这个功能工作吗?在
hashlib.md5
创建的对象不接受file对象。您需要一次向它提供一段数据,然后请求哈希摘要。在您需要读取文件:
您可能需要以二进制文件(“rb”)打开文件,然后分块读取数据块。ISO文件可能太大,无法放入内存中。在
相关问题 更多 >
编程相关推荐