n Random Key Generation with Keygen | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Keygen is a PHP package for generating simple random character sequences of any desired length. It ships with four generators, namely: numericalphanumerictoken and bytes. It has a very simple interface and supports method chaining — making it possible to generate simple random keys with just one line of code.

The Keygen package can be installed easily with [Composer] - require the gladcodes/keygen package from the command line.

$ composer require gladcodes/keygen

Alternatively, you can manually add the Keygen package to the composer.json file of your project and then run composer install from the command line as follows:

{
    "require": {
        "gladcodes/keygen": "~1.1"
    }
}
$ composer install

You can use it in your PHP code like this:

<?php

require __DIR__ . '/vendor/autoload.php';
use Keygen\Keygen;

printf("Your appID is %.0f", Keygen::numeric(12)->generate()); // Your appID is 878234290135

 

More Examples:

 

<?php

require __DIR__ . '/vendor/autoload.php';
use Keygen\Keygen;

$id_12 = Keygen::numeric(12)->generate();
$id_16 = Keygen::numeric()->generate();

$alnum = Keygen::alphanum(15)->generate();
$token = Keygen::token(28)->generate();

$bytes = Keygen::bytes(20)->generate();
$hex = Keygen::bytes(20)->hex()->generate();

echo $id_12; // 011683218639
echo $id_16; // 9352941287643963

echo $alnum; // TFd5X74Pr9ZOiG2
echo $token; // 4HE1xQz+4ks0Td128KSO/kBivd79

echo $bytes; // (some random byte chars)
echo $hex; // 9f802a80aaf4b5e89e14

 

Using Key Affixes and Transformations

The Keygen package provides two methods: prefix() and suffix(), for adding affixes to the beginning and/or end of generated keys. Each method takes a string 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 optional boolean value as the first parameter of the generate() method. The value truedisables inclusive affixes. If the boolean value is omitted, it defaults to false (enables inclusive affixes). Here is a simple demonstration.

<?php

require __DIR__ . '/vendor/autoload.php';
use Keygen\Keygen;

// 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 also 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.

Each transformation is executed on the generated key in the same order they are specified before the key is returned. Here is a simple example.

<?php

require __DIR__ . '/vendor/autoload.php';
use Keygen\Keygen;

$splitString = function($key) {
    return join('-', str_split($key, 4));
};

$shortHash20 = function($key) {
    return substr(md5($key), mt_rand(0,8), 20);
};

$reference = Keygen::numeric(20)->generate($splitString);

$filename = Keygen::bytes()->suffix('.png')->generate(true, ['strrev', $shortHash20], 'strtoupper');

echo $reference; // 2129-1489-0676-5152-9337
echo $filename; // 159D702E346F74E3F0B6.png

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook