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.
composer require componenta/file-hasheruse 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; // sha256withAlgorithm() returns a new immutable instance.
interface FileHasherInterface
{
public function hashFile(string $filename): string;
}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. |
| Package | Why it matters here |
|---|---|
componenta/hasher |
Hashes strings and PSR-7 streams. |
componenta/password |
Hashes passwords with PHP's dedicated password API. |