PHP code example of farhad-arjmand / lumen-hash-generator

1. Go to this page and download the library: Download farhad-arjmand/lumen-hash-generator 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/ */

    

farhad-arjmand / lumen-hash-generator example snippets


use FarhadArjmand\LumenHashGenerator\TokenGenerator;
use FarhadArjmand\LumenHashGenerator\TokenHasher;

$generator = new TokenGenerator();
$token = $generator->generate(prefix: 'api_'); // api_ + 32 random characters

$hasher = new TokenHasher();
$digest = $hasher->digest($token);            // Store this, not the bearer token.
$valid = $hasher->verify($token, $digest);     // Constant-time digest comparison.

$generator->generate();
$generator->hex(bytes: 32, prefix: 'secret_');
$generator->base64Url(bytes: 32);
$generator->generate(26, TokenGenerator::HUMAN_READABLE, 'invite_');
$generator->batch(count: 10, prefix: 'key_');

$hasher = new TokenHasher();
$plainText = $generator->base64Url();
$storedDigest = $hasher->digest($plainText);

// Persist the digest alongside owner, scope, expiration and revocation metadata.
// Return the plaintext token to the owner once, over HTTPS.

if (!$hasher->verify($providedToken, $storedDigest)) {
    // Reject the request.
}

$hasher = new TokenHasher(pepper: $keyFromYourSecretManager); // At least 32 bytes.

use FarhadArjmand\LumenHashGenerator\TokenGenerator;

final class CreateInvitation
{
    public function __construct(private TokenGenerator $tokens) {}

    public function handle(): string
    {
        return $this->tokens->generate(26, TokenGenerator::HUMAN_READABLE, 'invite_');
    }
}
sh
composer install
composer test
composer lint
php examples/basic.php
sh
php artisan vendor:publish --tag=hash-generator-config