/app/library/UUID.php

https://gitlab.com/michaelardian-dev/soccerStadiumReservation · PHP · 154 lines · 55 code · 28 blank · 71 comment · 4 complexity · dbecd80fa0d8fc5569fc1c40ff6ce3a4 MD5 · raw file

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