hylianshield / key-generator
生成序列号/API密钥。
1.0.1
2017-01-29 17:49 UTC
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-08-29 04:42:56 UTC
README
生成、编码和解码序列号和API密钥。
安装
composer require hylianshield/key-generator:^1.0.0
命令行
要查看描述的功能的完整结果,请尝试以下操作
composer encode-key $(composer decode-key $(composer generate-key))
输出将类似于
> ./bin/generate-key
> ./bin/decode-key 'EF0M508N-FZ78BSC1-GJRJKQTG-K01FGZ7Z-KQK2F81H'
> ./bin/encode-key '497163600149' '548925728129' '569907994448' '652884868351' '678171222065'
EF0M508N-FZ78BSC1-GJRJKQTG-K01FGZ7Z-KQK2F81H
用法
<?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'
请注意,在编码或生成密钥时,数值序列将存储在密钥对象中。当手动创建密钥并提供字符串时,密钥不会自动解码为数值序列。这是为了提高性能。
安全性
数字是使用数字生成器的实现生成的。默认情况下,它使用PHP 7中引入的CSPRNG实现来生成数字。
要更好地控制密钥生成,请尝试实现自定义数字生成器。
<?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() );
为了更具体和细粒度的控制,创建自定义数字序列并将其编码。
<?php use HylianShield\KeyGenerator\NumericalSequenceKey; /** HylianShield\KeyGenerator\KeyEncoderInterface $encoder */ $key = $encoder->encode( new NumericalSequenceKey(1, 2, 3, 4, 5) );