Using Python to compute a MD5 Hash
- Using Powershell to calculate/compare MD5 hashes
- Using Powershell to create MD5 files
- Using Python to compute a MD5 Hash
- Creating MD5 files with Python
Here some Python code to calculate the MD5 hash for a given file:
# from http://www.pythoncentral.io/hashing-files-with-python/ import hashlib hasher = hashlib.md5() with open('e:/Mappe1.xlsx', 'rb') as afile: buf = afile.read() hasher.update(buf) print(hasher.hexdigest()) |
Here the final script that provides the same functionality as the Powershell script I posted earlier today:
# http://www.pythoncentral.io/hashing-files-with-python/ import hashlib import os import codecs hasher = hashlib.md5() def calcHash(filename): print("Filename: " + filename) with open(filename, 'rb') as afile: buf = afile.read() hasher.update(buf) print("Calculated: " + hasher.hexdigest()) md5file = (os.path.splitext(filename)[0]) + ".md5" givenMD5 = codecs.open(md5file, "r", "utf-8-sig") print("Provided: " + str(givenMD5.read()).lower()) calcHash("e:/Mappe1.xlsx") |