Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
<?php
// EXAMPLE USAGE
//sn = "1AB%02X";
$digilist = "0123456789ABCDEFGHJKLMNPQRTUVWXY";
$tempID = NULL;
$tempID .= substr($digilist, rand(1, 9), 1); //random number
$tempID .= substr($digilist, rand(10, 31), 1); //then a letter
$tempID .= substr($digilist, rand(10, 31), 1); //another letter
$tempID .= ("%'.02X");
$i=0;
for ($i; $i < 99; $i+1)
{
$tempID = sprintf($tempID, $i);
$array = generate($tempID);
echo "\n";
echo "ID: " . $array[0];
echo "\n";
echo "Key: " . $array[1];
}
// FUNCTION
function generate(&$tempID)
{
$digilist = "0123456789ABCDEFGHJKLMNPQRTUVWXY";
//now we generate a new random ID number using the substrings of the digitList string above
$id = NULL;
$id .= $tempID;
//ok so now we need to generate an MD5 hash of our ID
$hash = md5($id);
//cycle through the hash 16 (length of key) times (in steps of 2 because each hex bytes is 2 digits long)
$i = 0;
$key = NULL;
for ($i; $i < 32; $i+=2)
{
//here we convert the next hex value to an integer and perform a bitwise AND operation against '31'
//31 is the highest substring value in our digit list.
$nextdigit = hexdec(substr($hash, $i, 2)) & 31;
//if 'i' is divisable by 8 (every 4 cycles) then we want to add "-"
if ((($i % 8) == 0) && ($i > 0))
{
$key .= "-".substr($digilist, $nextdigit, 1);
}
else
{
$key .= substr($digilist, $nextdigit, 1);
}
}
$array = array($id, $key);
//return
return $array;
}
?>
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more