PHP code example of units / hasher

1. Go to this page and download the library: Download units/hasher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

units / hasher example snippets


use Units\Hasher\Hasher;

$hasher = Hasher::create();                 // sha256, hex

$hasher->sha256('hello');                   // 2cf24dba…b9824
$hasher->hmac('payload', $secret)->hash;    // 0e7320e5…6ac3 — keyed
$hasher->compare($a, $b);                   // true — safe, use this not ===
$hasher->verify('hello', $known);           // true — hashes, then compares

$hasher->hash($value)->hash;         // store this
$hasher->verify($value, $stored);    // check it later — true

$expected = $hasher->hmac($body, $secret)->hash;

$hasher->compare($expected, $signature);   // true — authentic

$hasher->compare($a, $b);         // timing-safe, length-checked
$hasher->verify($value, $known);  // hashes first; never throws

$hasher->hash($value, ['salt' => $hasher->salt()])->hash;

$hasher->hash('hello', ['encoding' => 'base64url'])->hash;

$hasher = Hasher::create(['algorithm' => 'sha512']);

$hasher->hash('hello', ['algorithm' => 'sha1'])->algorithm;   // 'sha1', just once

$hasher->algorithms();          // all seven, in that order
$hasher->supports('sha3-512');  // true only if this PHP was built with it

// an application composing both
$peppered = Hasher::create()->hmac($password, $pepper)->hash;
$stored   = Password::create(['algorithm' => 'argon2id'])->hash($peppered)->hash;

$record = ['digest' => $hasher->hash($value)->hash, 'dna' => $hasher->dna()->toArray()];

Hasher::schema()->label();     // Hasher v1.0.0
Hasher::schema()->version();   // '1.0.0' - which code produced this digest