Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. function guid()
  2. {
  3.     $randomString = openssl_random_pseudo_bytes(16);
  4.     $time_low = bin2hex(substr($randomString, 0, 4));
  5.     $time_mid = bin2hex(substr($randomString, 4, 2));
  6.     $time_hi_and_version = bin2hex(substr($randomString, 6, 2));
  7.     $clock_seq_hi_and_reserved = bin2hex(substr($randomString, 8, 2));
  8.     $node = bin2hex(substr($randomString, 10, 6));
  9.  
  10.     /**
  11.      * Set the four most significant bits (bits 12 through 15) of the
  12.      * time_hi_and_version field to the 4-bit version number from
  13.      * Section 4.1.3.
  14.      * @see http://tools.ietf.org/html/rfc4122#section-4.1.3
  15.     */
  16.     $time_hi_and_version = hexdec($time_hi_and_version);
  17.     $time_hi_and_version = $time_hi_and_version >> 4;
  18.     $time_hi_and_version = $time_hi_and_version | 0x4000;
  19.  
  20.     /**
  21.      * Set the two most significant bits (bits 6 and 7) of the
  22.      * clock_seq_hi_and_reserved to zero and one, respectively.
  23.      */
  24.     $clock_seq_hi_and_reserved = hexdec($clock_seq_hi_and_reserved);
  25.     $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2;
  26.     $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000;
  27.  
  28.     return sprintf('%08s-%04s-%04x-%04x-%012s', $time_low, $time_mid, $time_hi_and_version, $clock_seq_hi_and_reserved, $node);
  29. } // guid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement