PHP code example of hylianshield / key-generator

1. Go to this page and download the library: Download hylianshield/key-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/ */

    

hylianshield / key-generator example snippets



use HylianShield\KeyGenerator\Key;
use HylianShield\KeyGenerator\NumericalSequenceKey;
use HylianShield\KeyGenerator\KeyGenerator;
use HylianShield\NumberGenerator\NumberGenerator;
use HylianShield\Encoding\Base32CrockfordEncoder;

$generator = new KeyGenerator(
    new NumberGenerator(),
    new Base32CrockfordEncoder()
);

// Keys can be interpreted as string.
echo $generator->generateKey(3); // 'HR0ZCHTF-TSDMKNRX-HQK5EJZQ'

// Keys can be decoded back to a numerical sequence.
$sequence = $generator->decode(
    new Key('HR0ZCHTF-TSDMKNRX-HQK5EJZQ')
);

// And keys can be encoded from a numerical sequence.
$key = $generator->encode(
    new NumericalSequenceKey(609918273359, 920654567197, 609454869495)    
);

// Key output will be the same for any given numerical sequence.
echo $key;                                       // 'HR0ZCHTF-TSDMKNRX-HQK5EJZQ'
echo implode(' ', $key->getNumericalSequence()); // '609918273359 920654567197 609454869495'


use HylianShield\KeyGenerator\KeyGenerator;
use HylianShield\NumberGenerator\NumberGenerator;
use HylianShield\Encoding\Base32CrockfordEncoder;

$generator = new KeyGenerator(
    new class extends NumberGenerator
    {
        public function generateNumber(int $min = 0, int $max = PHP_INT_MAX): int
        {
            // Do not actually use this implementation.
            // It is there for example purposes only.
            return mt_rand($min, $max);
        }
    },
    new Base32CrockfordEncoder()
);


use HylianShield\KeyGenerator\NumericalSequenceKey;

/** HylianShield\KeyGenerator\KeyEncoderInterface $encoder */
$key = $encoder->encode(
    new NumericalSequenceKey(1, 2, 3, 4, 5)
);