Computing MD5 Hashes for Large Files in Python
Python's hashlib module provides a convenient interface for calculating cryptographic hashes. However, for exceptionally large files whose size exceeds system memory, using hashlib directly can be problematic.
Solution: Progressive Hashing
To address this issue, we employ progressive hashing by reading the file in manageable chunks. This approach ensures that the entire file content is hashed without consuming excessive memory. Here's a sample Python function that implements this technique:
import hashlib
def md5_for_file(f):
block_size = 2**20
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.digest()
To calculate the MD5 hash of a large file, you can invoke the function as follows:
with open("filename", "rb") as f:
md5 = md5_for_file(f)
Note on File Mode
Ensure that you open the file in binary mode with "rb" for accurate results. Using "r" can lead to incorrect calculations.
Additional Considerations
For convenience, an improved version of the function is presented below:
import hashlib
import os
def generate_file_md5(rootdir, filename):
m = hashlib.md5()
with open(os.path.join(rootdir, filename), "rb") as f:
buf = f.read()
while buf:
m.update(buf)
buf = f.read()
return m.hexdigest()
Cross-checking the calculated hashes with external tools like jacksum is recommended to verify accuracy.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3