UUID.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Tools {
  3. /**
  4. * Usage
  5. *
  6. * Named-based UUID
  7. * $v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  8. * $v5uuid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  9. *
  10. * Pseudo-random UUID
  11. * $v4uuid = UUID::v4();
  12. */
  13. class UUID {
  14. public static function v3($namespace, $name) {
  15. if(!self::is_valid($namespace)) return false;
  16. // Get hexadecimal components of namespace
  17. $nhex = str_replace(array('-','{','}'), '', $namespace);
  18. // Binary Value
  19. $nstr = '';
  20. // Convert Namespace UUID to bits
  21. for($i = 0; $i < strlen($nhex); $i+=2) {
  22. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  23. }
  24. // Calculate hash value
  25. $hash = md5($nstr . $name);
  26. return sprintf('%08s-%04s-%04x-%04x-%12s',
  27. // 32 bits for "time_low"
  28. substr($hash, 0, 8),
  29. // 16 bits for "time_mid"
  30. substr($hash, 8, 4),
  31. // 16 bits for "time_hi_and_version",
  32. // four most significant bits holds version number 3
  33. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  34. // 16 bits, 8 bits for "clk_seq_hi_res",
  35. // 8 bits for "clk_seq_low",
  36. // two most significant bits holds zero and one for variant DCE1.1
  37. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  38. // 48 bits for "node"
  39. substr($hash, 20, 12)
  40. );
  41. }
  42. public static function v4() {
  43. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  44. // 32 bits for "time_low"
  45. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  46. // 16 bits for "time_mid"
  47. mt_rand(0, 0xffff),
  48. // 16 bits for "time_hi_and_version",
  49. // four most significant bits holds version number 4
  50. mt_rand(0, 0x0fff) | 0x4000,
  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. mt_rand(0, 0x3fff) | 0x8000,
  55. // 48 bits for "node"
  56. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  57. );
  58. }
  59. public static function v5($namespace, $name) {
  60. if(!self::is_valid($namespace)) return false;
  61. // Get hexadecimal components of namespace
  62. $nhex = str_replace(array('-','{','}'), '', $namespace);
  63. // Binary Value
  64. $nstr = '';
  65. // Convert Namespace UUID to bits
  66. for($i = 0; $i < strlen($nhex); $i+=2) {
  67. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  68. }
  69. // Calculate hash value
  70. $hash = sha1($nstr . $name);
  71. return sprintf('%08s-%04s-%04x-%04x-%12s',
  72. // 32 bits for "time_low"
  73. substr($hash, 0, 8),
  74. // 16 bits for "time_mid"
  75. substr($hash, 8, 4),
  76. // 16 bits for "time_hi_and_version",
  77. // four most significant bits holds version number 5
  78. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  79. // 16 bits, 8 bits for "clk_seq_hi_res",
  80. // 8 bits for "clk_seq_low",
  81. // two most significant bits holds zero and one for variant DCE1.1
  82. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  83. // 48 bits for "node"
  84. substr($hash, 20, 12)
  85. );
  86. }
  87. public static function is_valid($uuid) {
  88. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
  89. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  90. }
  91. }
  92. }
  93. ?>