-
Notifications
You must be signed in to change notification settings - Fork 19
Usage
The Keygen package ships with four built-in key generators for four key types namely:
numeric
,alphanumeric
,token
andbyte
- and exposes methods to access these key generators. Each method can take an optional$length
parameter which specifies the required key length. If the$length
parameter is omitted, it defaults to16
. Thegenerate()
method must be called to return the generated key.
// numeric() creates a numeric key generator
// e.g 3105746962798934
echo Keygen::numeric()->generate(); // length => 16
// alphanum() creates an alphanumeric key generator
// e.g 70f9V9mdaz1Qtr8L
echo Keygen::alphanum()->generate(); // length => 16
// bytes() creates a random bytes generator
// e.g �K����Y�v����������C"�Ѣ��G
echo Keygen::bytes(32)->generate(); // length => 32
// token() creates a base64-encoded token generator
// e.g hav95WQV68f9RnknApV+8Mki
echo Keygen::token(24)->generate(); // length => 24
The Keygen package provides a convenient method for returning generated random bytes in hexadecimal form. Simply append the
hex()
method to abytes()
generator before generating the key. The length of the resulting hexadecimal string is the same as the key length of the bytes generator.
// As Binary
echo Keygen::bytes()->generate(); // e.g ���E���)�B�%^$^
// As Hexadecimal
echo Keygen::bytes(20)->hex()->generate(); // e.g 8cd681c009e97ad2550a
The Keygen package provides two methods:
prefix()
andsuffix()
, for adding affixes to the beginning and/or end of generated keys. Each method takes astring
as the first parameter which specifies the affix.
When affixes are specified in a generator, the length of the generated key includes the string length of the affixes (
inclusive affixes
). This behaviour can be controlled by passing an optionalboolean
value as the first parameter of thegenerate()
method. The valuetrue
disablesinclusive affixes
. If the boolean value is omitted, it defaults tofalse
(enablesinclusive affixes
).
// Prefix (inclusive enabled)
echo Keygen::numeric(12)->prefix('TM-')->generate(); // e.g TM-218624395
// Prefix (inclusive disabled)
echo Keygen::numeric(12)->prefix('TM-')->generate(true); // e.g TM-382104609735
// Suffix (inclusive enabled)
echo Keygen::alphanum(20)->suffix('.me')->generate(); // e.g 5UpwTe7cl268s31c9.me
// Suffix (inclusive disabled)
echo Keygen::alphanum(20)->suffix('.me')->generate(true); // e.g X1QkHm5OAI9q3F3VxNv8.me
// Combined (inclusive enabled)
echo Keygen::numeric(15)->prefix('TM-')->suffix('.me')->generate(); // e.g TM-101636457.me
The Keygen package allows for one or more transformations to be applied on keys before they are generated. A transformation is simply a callable that can take the generated key as the first argument and returns a string. One or more transformations can be applied by passing them as arguments to the
generate()
method. If the key contains one or more affixes, the optionalboolean
first parameter of thegenerate()
method may still be specified before specifying the transformations.
// Using bin2hex()
echo Keygen::bytes()->generate('bin2hex'); // e.g df67ae24a1289030730adde51412771b
// Using custom transformation
// e.g 3530-8167-4572-2620-1049
echo Keygen::numeric(20)->generate(function($key) {
return join('-', str_split($key, 4));
});
// Multiple transformations with suffix (inclusive disabled)
// e.g B8510150F67BD067BC9E.img
echo Keygen::bytes()->suffix('.img')->generate(true, ['strrev', function($key) {
return substr(md5($key), mt_rand(0,8), 20);
}], 'strtoupper');