Integrity Complete Examples#
This page groups Integrity examples by what is being checked: strings, files and support helpers.
Hash and Verify a String#
<?php
declare(strict_types=1);
use Infocyph\Epicrypt\Integrity\StringHasher;
$stringHasher = new StringHasher('sha256');
$digest = $stringHasher->hash('payload');
$digestValid = $stringHasher->verify('payload', $digest);
Use an Alternate Algorithm#
<?php
declare(strict_types=1);
use Infocyph\Epicrypt\Integrity\StringHasher;
$blake = (new StringHasher('blake2b'))->hash('payload', ['length' => 32]);
Hash and Verify a File#
<?php
declare(strict_types=1);
use Infocyph\Epicrypt\Integrity\FileHasher;
$fileHasher = new FileHasher('sha256');
$fileDigest = $fileHasher->hash('/tmp/payload.txt');
$fileDigestValid = $fileHasher->verify('/tmp/payload.txt', $fileDigest);
Use Support Helpers#
Use helpers for canonical fingerprints and timing-safe equality checks.
<?php
declare(strict_types=1);
use Infocyph\Epicrypt\Integrity\Support\ContentFingerprinter;
use Infocyph\Epicrypt\Integrity\Support\TimingSafeComparator;
$fingerprint = (new ContentFingerprinter())->fingerprint('payload', ['a' => '1', 'b' => '2']);
$same = (new TimingSafeComparator())->equals('known', 'known');