hylianshield / key-generator
Generate serials / API keys.
Installs: 5 359
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: ^7.0
- hylianshield/base32-crockford: ^1.0.1
- hylianshield/number-generator: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.7.5
This package is auto-updated.
Last update: 2024-11-29 05:09:32 UTC
README
Generate, encode and decode serials and API keys.
Installation
composer require hylianshield/key-generator:^1.0.0
Command line
To see a complete result of the described functionality, try the following:
composer encode-key $(composer decode-key $(composer generate-key))
The output will be similar to:
> ./bin/generate-key
> ./bin/decode-key 'EF0M508N-FZ78BSC1-GJRJKQTG-K01FGZ7Z-KQK2F81H'
> ./bin/encode-key '497163600149' '548925728129' '569907994448' '652884868351' '678171222065'
EF0M508N-FZ78BSC1-GJRJKQTG-K01FGZ7Z-KQK2F81H
Usage
<?php 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'
Note that when encoding or generating a key, the numerical sequence will be stored in the key object. When a key is created manually, by supplying a string, the key will not be automatically decoded into a numerical sequence. This is to improve performance.
Security
The numbers are generated using an implementation of a number generator. By default, it generates numbers using the CSPRNG implementation introduced in PHP 7.
For more control over key generation, try implementing a custom number generator.
<?php 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() );
For specific and fine-grained control, create a custom number sequence and let that be encoded.
<?php use HylianShield\KeyGenerator\NumericalSequenceKey; /** HylianShield\KeyGenerator\KeyEncoderInterface $encoder */ $key = $encoder->encode( new NumericalSequenceKey(1, 2, 3, 4, 5) );