Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Hashlib MD5 differs between linux/windows

Tags:

python

I have a python app in which I am creating packages in windows to be used and later compared in a linux python app. I am creating an md5 for a file in windows to be checked later in linux. The problem is that the same code on the same file gives different Md5 hash results in each environment. Below is the method I use to calculate the Md5. (It is the same code on each end, and I am using Python 2.6.5 for both windows/linux environments) When I run this on the same file in different environments, I get md5 hashes that do not match.

def md5_for_file(filePath):
        md5 = hashlib.md5()
        file = open(filePath)
        while True:
            data = file.read(8192)
            if not data:
                break
            md5.update(data)

        file.close()   
        return md5.hexdigest()

Any ideas or suggestions are appreciated.

like image 419
Tom Lerma Avatar asked Aug 02 '10 18:08

Tom Lerma


1 Answers

Change open(filePath) to open(filePath, 'rb'), where the b is for binary mode. You're currently opening in text mode, which can cause portability issues.

like image 154
Cory Petosky Avatar answered Oct 14 '22 17:10

Cory Petosky