Filehash
Python module and command-line tool that wraps around `hashlib` and `zlib` to facilitate generating checksums/hashes of files and directories. It supports various algorithms like MD5, SHA-*, Adler-32, and CRC32, handling large files efficiently by processing them in chunks. The current version is 0.2.dev1 and it appears to be in active development based on the versioning.
Warnings
- deprecated MD5 and SHA-1 algorithms are supported by `filehash` but are cryptographically weak and should be avoided for new security-sensitive applications. They are susceptible to collision attacks.
- gotcha The BLAKE2b and BLAKE2s algorithms are only supported on Python 3.6.x and later. Attempting to use them on older Python versions will result in an error due to underlying `hashlib` limitations.
- gotcha The `filehash` library is designed to efficiently handle large files by reading them in chunks. While a default `chunk_size` is used, for extremely large files or specific performance requirements, you might need to manually adjust the `chunk_size` parameter in the `FileHash` constructor.
Install
-
pip install filehash
Imports
- FileHash
from filehash import FileHash
Quickstart
import os
import tempfile
from filehash import FileHash
# Create a temporary file for demonstration
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as temp_file:
temp_file.write('This is a temporary file for hashing demonstration.')
temp_file.write('\nAnother line of content.')
temp_file_path = temp_file.name
try:
# Initialize FileHash with a desired algorithm (defaults to 'sha256')
file_hasher = FileHash('sha256')
# Calculate the hash of the temporary file
file_hash = file_hasher.hash_file(temp_file_path)
print(f"SHA256 hash of '{os.path.basename(temp_file_path)}': {file_hash}")
# You can also specify other algorithms, e.g., MD5
file_hasher_md5 = FileHash('md5')
md5_hash = file_hasher_md5.hash_file(temp_file_path)
print(f"MD5 hash of '{os.path.basename(temp_file_path)}': {md5_hash}")
# Example of hashing a directory (requires creating dummy directory and files)
# For this quickstart, we'll keep it to single file hash.
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Clean up the temporary file
if os.path.exists(temp_file_path):
os.remove(temp_file_path)