Introduction to md5-file
md5-file
is a lightweight Python library that allows for easy calculation of MD5 hashes for files. This is particularly useful for verifying the integrity of files, as MD5 hashes can be used to check that files have not been altered or corrupted.
Installation
pip install md5-file
Basic Usage
Here is an example of basic usage for calculating the MD5 hash of a file.
import md5file
# Calculate the MD5 hash for a file
file_hash = md5file.calculate('path/to/your/file.txt')
print(f'MD5 hash: {file_hash}')
API Examples
Calculate MD5 with Buffer Size
You can specify a buffer size to optimize the hashing for larger files:
import md5file
buffer_size = 8192 # 8KB buffer size
file_hash = md5file.calculate('path/to/large/file.txt', buffer_size=buffer_size)
print(f'MD5 hash with buffer size: {file_hash}')
Verify File Integrity
Verify a file’s integrity by comparing its MD5 hash to a known hash:
import md5file
known_hash = '5eb63bbbe01eeed093cb22bb8f5acdc3'
file_hash = md5file.calculate('path/to/your/file.txt')
if file_hash == known_hash:
print('File integrity verified.')
else:
print('File integrity compromised!')
Calculate MD5 for Multiple Files
Calculate MD5 hashes for multiple files in a directory:
import os
import md5file
directory = 'path/to/your/directory'
files = [os.path.join(directory, f) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
for file_path in files:
file_hash = md5file.calculate(file_path)
print(f'File: {file_path}, MD5 hash: {file_hash}')
App Example
Below is an example of a simple Python application that makes use of the md5-file
library to monitor file integrity in a directory:
import os
import time
import md5file
def calculate_md5(file_path):
return md5file.calculate(file_path)
def monitor_directory(directory, interval=60):
file_hashes = {}
while True:
for file_name in os.listdir(directory):
file_path = os.path.join(directory, file_name)
if os.path.isfile(file_path):
file_hash = calculate_md5(file_path)
if file_name in file_hashes:
if file_hashes[file_name] != file_hash:
print(f"File {file_name} has been modified.")
else:
file_hashes[file_name] = file_hash
time.sleep(interval)
if __name__ == "__main__":
monitor_directory('path/to/your/directory')
By using the above code, you can monitor changes in files within a specific directory, ensuring their integrity over time.
Hash: db72603ad186cb4abafe5ef448372feb3e5dba56304945997609d8c420d9da1a