RapidMod

RapidmodDataGenerator

Mar 12th, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author RapidMod.com
  4.  * @author 813.330.0522
  5.  * @version 0.0.1
  6.  */
  7.  
  8.  
  9. namespace Rapidmod\Data;
  10.  
  11.  
  12. class Generator {
  13.  
  14.     public static $defaultStringLength = 16;
  15.  
  16.  
  17.     protected static $rfcSecondaryKeys = array(8,9,"a","b");
  18.  
  19.     /**
  20.      *
  21.      * Name guid
  22.      * @return
  23.      * @return string  36-character string "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  24.      *  blocks of 8, 4, 4, 4 and 12 hex digits
  25.      * @static static
  26.      * @throws
  27.      *
  28.      * @author RapidMod.io
  29.      * @author 813.330.0522
  30.      *
  31.      */
  32.     public static function guid(){
  33.         $c = self::randomString(16);
  34.         $h = "-";
  35.         return  substr($c, 0, 8).$h
  36.                   .substr($c, 8, 4).$h
  37.                   .substr($c,12, 4).$h
  38.                   .substr($c,16, 4).$h
  39.                   .substr($c,20,12);
  40.     }
  41.  
  42.     public static function randomString($length){
  43.         $length = (int)$length;
  44.         if( $length < 1 ){ $length = (int)self::$defaultStringLength; }
  45.         return bin2hex( random_bytes( $length ) );
  46.         //return md5(bin2hex( random_bytes( $length ) ));
  47.     }
  48.  
  49.     /**
  50.      *
  51.      * Name uuid
  52.      * @return string  formatted for RFC 4122 36-character string "XXXXXXXX-XXXX-4XXX-(8,9,a,b)XXX-XXXXXXXXXXXX" see link below
  53.      *  blocks of 8, 4, 4, 4 and 12 hex digits
  54.      * @static static
  55.      * @throws
  56.      *
  57.      * @author RapidMod.io
  58.      * @author 813.330.0522
  59.      *
  60.      * @link  https://www.cryptosys.net/pki/uuid-rfc4122.html
  61.      * @link http://www.ietf.org/rfc/rfc4122.txt
  62.      */
  63.     public static function uuid(){
  64.         $c = self::randomString(16);
  65.         $c[12] = 4;
  66.         $x = random_int(0,3);
  67.         $c[16] = self::$rfcSecondaryKeys[$x];
  68.         $h = chr(45);
  69.         return  substr($c, 0, 8).$h
  70.                   .substr($c, 8, 4).$h
  71.                   .substr($c,12, 4).$h
  72.                   .substr($c,16, 4).$h
  73.                   .substr($c,20,12);
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment