PHP - Generate UUID


SUBMITTED BY: Guest

DATE: May 28, 2013, 12:04 p.m.

FORMAT: PHP

SIZE: 4.8 kB

HITS: 1081

  1. UUID.php
  2. ---------------------------------------------
  3. <?php
  4. /**
  5. * UUID class
  6. *
  7. * The following class generates VALID RFC 4211 COMPLIANT
  8. * Universally Unique IDentifiers (UUID) version 3, 4 and 5.
  9. *
  10. * UUIDs generated validates using OSSP UUID Tool, and output
  11. * for named-based UUIDs are exactly the same. This is a pure
  12. * PHP implementation.
  13. *
  14. * @author Andrew Moore
  15. * @link http://www.php.net/manual/en/function.uniqid.php#94959
  16. */
  17. class UUID
  18. {
  19. /**
  20. * Generate v3 UUID
  21. *
  22. * Version 3 UUIDs are named based. They require a namespace (another
  23. * valid UUID) and a value (the name). Given the same namespace and
  24. * name, the output is always the same.
  25. *
  26. * @param uuid $namespace
  27. * @param string $name
  28. */
  29. public static function v3($namespace, $name)
  30. {
  31. if(!self::is_valid($namespace)) return false;
  32. // Get hexadecimal components of namespace
  33. $nhex = str_replace(array('-','{','}'), '', $namespace);
  34. // Binary Value
  35. $nstr = '';
  36. // Convert Namespace UUID to bits
  37. for($i = 0; $i < strlen($nhex); $i+=2)
  38. {
  39. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  40. }
  41. // Calculate hash value
  42. $hash = md5($nstr . $name);
  43. return sprintf('%08s-%04s-%04x-%04x-%12s',
  44. // 32 bits for "time_low"
  45. substr($hash, 0, 8),
  46. // 16 bits for "time_mid"
  47. substr($hash, 8, 4),
  48. // 16 bits for "time_hi_and_version",
  49. // four most significant bits holds version number 3
  50. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  51. // 16 bits, 8 bits for "clk_seq_hi_res",
  52. // 8 bits for "clk_seq_low",
  53. // two most significant bits holds zero and one for variant DCE1.1
  54. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  55. // 48 bits for "node"
  56. substr($hash, 20, 12)
  57. );
  58. }
  59. /**
  60. *
  61. * Generate v4 UUID
  62. *
  63. * Version 4 UUIDs are pseudo-random.
  64. */
  65. public static function v4()
  66. {
  67. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  68. // 32 bits for "time_low"
  69. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  70. // 16 bits for "time_mid"
  71. mt_rand(0, 0xffff),
  72. // 16 bits for "time_hi_and_version",
  73. // four most significant bits holds version number 4
  74. mt_rand(0, 0x0fff) | 0x4000,
  75. // 16 bits, 8 bits for "clk_seq_hi_res",
  76. // 8 bits for "clk_seq_low",
  77. // two most significant bits holds zero and one for variant DCE1.1
  78. mt_rand(0, 0x3fff) | 0x8000,
  79. // 48 bits for "node"
  80. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  81. );
  82. }
  83. /**
  84. * Generate v5 UUID
  85. *
  86. * Version 5 UUIDs are named based. They require a namespace (another
  87. * valid UUID) and a value (the name). Given the same namespace and
  88. * name, the output is always the same.
  89. *
  90. * @param uuid $namespace
  91. * @param string $name
  92. */
  93. public static function v5($namespace, $name)
  94. {
  95. if(!self::is_valid($namespace)) return false;
  96. // Get hexadecimal components of namespace
  97. $nhex = str_replace(array('-','{','}'), '', $namespace);
  98. // Binary Value
  99. $nstr = '';
  100. // Convert Namespace UUID to bits
  101. for($i = 0; $i < strlen($nhex); $i+=2)
  102. {
  103. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  104. }
  105. // Calculate hash value
  106. $hash = sha1($nstr . $name);
  107. return sprintf('%08s-%04s-%04x-%04x-%12s',
  108. // 32 bits for "time_low"
  109. substr($hash, 0, 8),
  110. // 16 bits for "time_mid"
  111. substr($hash, 8, 4),
  112. // 16 bits for "time_hi_and_version",
  113. // four most significant bits holds version number 5
  114. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  115. // 16 bits, 8 bits for "clk_seq_hi_res",
  116. // 8 bits for "clk_seq_low",
  117. // two most significant bits holds zero and one for variant DCE1.1
  118. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  119. // 48 bits for "node"
  120. substr($hash, 20, 12)
  121. );
  122. }
  123. public static function is_valid($uuid) {
  124. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
  125. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  126. }
  127. }
  128. ?>
  129. demo.php
  130. ---------------------------------------------
  131. <?php
  132. // Usage
  133. include 'UUID.php';
  134. // Named-based UUID.
  135. $v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  136. $v5uuid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  137. // Pseudo-random UUID
  138. $v4uuid = UUID::v4();
  139. ?>

comments powered by Disqus