componenta / file-hasher
File hashing contract and default implementation
v1.0.0
2026-06-17 00:51 UTC
Requires
- php: ^8.4
Requires (Dev)
- pestphp/pest: ^4.0
- phpunit/phpunit: ^12.0
README
File hashing contract and default implementation.
Use this package when a service needs to hash a file by filename and receive file-specific exceptions for invalid paths.
Installation
composer require componenta/file-hasher
Usage
use Componenta\Stdlib\FileHasher; $hasher = new FileHasher('sha256'); $hash = $hasher->hashFile(__DIR__ . '/document.pdf'); $sha1 = $hasher->withAlgorithm('sha1');
FileHasher exposes the selected algorithm as read-only public state:
$hasher->algorithm; // sha256
withAlgorithm() returns a new immutable instance.
Contract
interface FileHasherInterface { public function hashFile(string $filename): string; }
Exceptions
All package exceptions are named with a File* prefix and implement FileHasherExceptionInterface.
| Exception | When it is thrown |
|---|---|
FileNotFoundException |
The path does not exist. |
FileNotReadableException |
The path exists but is not a readable regular file. |
FileHashFailedException |
PHP failed to compute the file hash. |
Related Packages
| Package | Why it matters here |
|---|---|
componenta/hasher |
Hashes strings and PSR-7 streams. |
componenta/password |
Hashes passwords with PHP's dedicated password API. |