How to Hash Large Files in Python without Memory Overconsumption?

Barbara Streisand
Release: 2024-10-20 11:06:30
Original
818 people have browsed it

How to Hash Large Files in Python without Memory Overconsumption?

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:

<code class="python">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()</code>
Copy after login

To calculate the MD5 hash of a large file, you can invoke the function as follows:

<code class="python">with open("filename", "rb") as f:
    md5 = md5_for_file(f)</code>
Copy after login

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:

<code class="python">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()</code>
Copy after login

Cross-checking the calculated hashes with external tools like jacksum is recommended to verify accuracy.

The above is the detailed content of How to Hash Large Files in Python without Memory Overconsumption?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!