asciicat

mbam

Dec 20th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 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.       //more info on bitwise ops here: Bitwise operation - Wikipedia, the free encyclopedia
  33.       $nextdigit = hexdec(substr($hash, $i, 2)) & 31;
  34.  
  35.       //if 'i' is divisable by 8 (every 4 cycles) then we want to add "-"
  36.       if ((($i % 8) == 0) && ($i > 0))
  37.       {
  38.          $key .= "-".substr($digilist, $nextdigit, 1);
  39.       }
  40.       else
  41.       {
  42.          $key .= substr($digilist, $nextdigit, 1);
  43.       }
  44.    }
  45.  
  46.    $array = array($id, $key);
  47.    //return
  48.    return $array;
  49. }
  50. ?>
Add Comment
Please, Sign In to add comment