2
0

uuid.php 3.2 KB

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