如何用Python找到ISO文件的MD5哈希值?

3 投票
2 回答
1993 浏览
提问于 2025-04-16 21:44

我正在写一个简单的工具,目的是让我能快速检查下载的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()

我遇到的问题出现在第9行,当我尝试获取文件的MD5哈希值时,出现了类型错误:需要支持缓冲区API的对象。有没有人能帮我解释一下,怎么才能让这个功能正常工作呢?

2 个回答

3

你需要读取这个文件:

import sys
import hashlib

def main():
    filename = sys.argv[1] # Takes the ISO 'file' as an argument in the command line
    testFile = open(filename, "rb") # Opens and reads the ISO 'file'

    # Use hashlib here to find MD5 hash of the ISO 'file'. This is where I'm having problems
    m = hashlib.md5()
    while True:
        data = testFile.read(4*1024*1024)
        if not data: break
        m.update(data)
    hashedMd5 = m.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()

而且你可能需要以二进制模式打开这个文件(用“rb”),然后分块读取数据。因为一个ISO文件通常太大,无法一次性放进内存里。

8

通过 hashlib.md5 创建的对象不能直接接受文件对象。你需要一块一块地把数据传给它,然后再请求得到哈希值。

import hashlib

testFile = open(filename, "rb")
hash = hashlib.md5()

while True:
    piece = testFile.read(1024)

    if piece:
        hash.update(piece)
    else: # we're at end of file
        hex_hash = hash.hexdigest()
        break

print hex_hash # will produce what you're looking for

撰写回答