Skip to content

Instantly share code, notes, and snippets.

@relliv
Created April 28, 2018 08:08
Show Gist options
  • Save relliv/2f352503b6a63713b426654f92bec83a to your computer and use it in GitHub Desktop.
Save relliv/2f352503b6a63713b426654f92bec83a to your computer and use it in GitHub Desktop.
PHP Unique License Key Generator
<?php
// some input and date
$date = date("Y-m-d H:i:s");
$hashsubject = "[email protected] - {$date}";
$hash = strtoupper(hash('sha512', $hashsubject));
// parse hash with pettern
$blocksize = 5;
$licpettern = "/(([A-Z0-9]{".$blocksize."})){5}/";
preg_match_all($licpettern, $hash, $mmatches);
// get block and parse with block pettern
$licblockpettern = "/([A-Z0-9]{".$blocksize."})/";
preg_match_all($licblockpettern, $mmatches[0][0], $matchess);
//print_r($mmatches);
// create new license code
echo implode('-', $matchess[0]);
/*
with $blocksize = ?
recommended = 5
-> Permutation(37,5): 52 307 640 possibility
output: 383E3-47919-11656-CC4BF-00C66
-> Permutation(37,6): 1 673 844 480 possibility
output: 6CD498-5AE9AD-DC8C11-7C1A57-B6C550
-> Permutation(37,7): 51 889 178 880 possibility
output: 7AB8D70-6838F09-4389AF5-392BB3D-A56AA00
max = 25 (about hash512 result lenght 128 characters and 128 / 5 (block) = ~25)
...
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment