Registry / serialization / filehash

filehash

JSON →
library0.2.dev1pypypi✓ verified 86d ago

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.

pip install filehash
INSTALL
IMPORT
SIG · FILEHASH
F
filehash
serializationpythonv0.2.dev1
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.dev1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.010s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FileHash
from filehash import FileHash

This quickstart demonstrates how to initialize the `FileHash` class and calculate the hash of a single file using different algorithms like SHA256 and MD5. It creates a temporary file for a runnable example and cleans it up afterwards. The `FileHash` constructor also accepts an optional `chunk_size` parameter for fine-tuning performance with very large files.

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)
filehash --version
Debug
Known issues
deprecatedMD5 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.
fix
Prefer stronger cryptographic hash algorithms like SHA-256, SHA-512, or BLAKE2* for integrity checks in security-sensitive contexts.
affects: All versions
gotchaThe 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.
fix
Ensure your Python environment is 3.6 or newer when utilizing BLAKE2* algorithms. Otherwise, select a different hash algorithm compatible with your Python version.
affects: <3.6.x
gotchaThe `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.
fix
Adjust `chunk_size` in `FileHash(chunk_size=desired_bytes)` for optimized performance or memory usage, especially with multi-gigabyte files.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'filehash'
The 'filehash' package is not installed in the current Python environment or the Python interpreter cannot find it.
fix
Install the package using pip: `pip install filehash`
AttributeError: 'str' object has no attribute 'read'
A function or method within the 'filehash' library that expects an opened file-like object (which has a `.read()` method) was instead passed a plain string, such as a file path.
fix
Ensure you are passing the correct type of object. If the function expects an open file, pass `open('your_file.txt', 'rb')`; if it expects a path, pass the string directly. Consult the 'filehash' library's documentation for the specific function's argument type.
FileNotFoundError: [Errno 2] No such file or directory: 'non_existent_file.txt'
The 'filehash' library attempted to access a file at a specified path, but no file was found at that location or the path was incorrect.
fix
Verify that the file path provided to the 'filehash' function is correct and that the file exists at that location.
ValueError: Unsupported hash algorithm 'unsupported_alg'
The hashing algorithm specified (e.g., 'unsupported_alg') is not recognized or supported by the 'filehash' library or the underlying `hashlib` module.
fix
Use one of the supported algorithms such as 'md5', 'sha1', 'sha256', 'sha512', 'adler32', or 'crc32'. Consult the 'filehash' documentation for the full list of supported algorithms.
Upgrade
Version history
0.2.dev1latest on PyPI · released Oct 11, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
filehash — pip install filehash · libregistry