Advertisement
Fromubiz

mbam

Oct 19th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. // EXAMPLE USAGE
  3. $array = generate();
  4. echo $array[0];
  5. echo "<br />";
  6. echo $array[1];
  7.  
  8.  
  9. // FUNCTION
  10. function generate()
  11. {
  12.         $digilist = "0123456789ABCDEFGHJKLMNPQRTUVWXY";
  13.  
  14.         //now we generate a new random ID number using the substrings of the digitList string above
  15.         $id = NULL;
  16.         $id .= substr($digilist, rand(1, 9), 1); //random number
  17.         $id .= substr($digilist, rand(10, 31), 1); //then a letter
  18.         $id .= substr($digilist, rand(10, 31), 1); //another letter
  19.         $id .= substr($digilist, rand(1, 9), 1); //a number
  20.         $id .= substr($digilist, rand(1, 9), 1); //and finally another number - simple :D
  21.                        
  22.     //ok so now we need to generate an MD5 hash of our ID
  23.         $hash = md5($id);
  24.  
  25.         //cycle through the hash 16 (length of key) times (in steps of 2 because each hex bytes is 2 digits long)
  26.         $i = 0;
  27.         $key = NULL;
  28.         for ($i; $i < 32; $i+=2)
  29.         {
  30.                 //here we convert the next hex value to an integer and perform a bitwise AND operation against '31'
  31.                 //31 is the highest substring value in our digit list.  
  32.                 $nextdigit = hexdec(substr($hash, $i, 2)) & 31;
  33.  
  34.                 //if 'i' is divisable by 8 (every 4 cycles) then we want to add "-"
  35.                 if ((($i % 8) == 0) && ($i > 0))
  36.                 {
  37.                         $key .= "-".substr($digilist, $nextdigit, 1);
  38.                 }
  39.                 else
  40.                 {
  41.                         $key .= substr($digilist, $nextdigit, 1);
  42.                 }
  43.         }
  44.  
  45.         $array = array($id, $key);
  46.         //return
  47.         return $array;
  48. }
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement