开发者

How do I find the MD5 hash of an ISO file using Python?

I am writing a simple tool that allows me to quickly check MD5 hash values of downloaded ISO files. Here is my algorithm:

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()

My problem is on the 9th line when I try to take the MD5 hash of the file. I'm getting the Type Error: object supporting the buffer API required. Could anyone shed some light on to how to make this function work?


The object created by hashlib.md5 doesn't take a file object. You need to feed it data a piece at a time, and then request the hash digest.

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


You need to read the file:

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()

And you probably need to open the file in binary ("rb") and read the blocks of data in chunks. An ISO file is likely too large to fit in memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜