/siberian/app/sae/modules/Fanwall/Model/UUID.php

https://github.com/Xtraball/SiberianCMS · PHP · 114 lines · 55 code · 6 blank · 53 comment · 4 complexity · 8f8873a45551db42be3e321b4691d557 MD5 · raw file

  1. <?php
  2. namespace Fanwall\Model;
  3. use Siberian\UUID as Siberian_UUID;
  4. /**
  5. * Class UUID
  6. * @package Fanwall\Model
  7. */
  8. class UUID extends Siberian_UUID
  9. {
  10. /**
  11. * @param $namespace
  12. * @param $name
  13. * @return bool|string
  14. */
  15. public static function v3($namespace, $name)
  16. {
  17. if (!self::is_valid($namespace)) return false;
  18. // Get hexadecimal components of namespace
  19. $nhex = str_replace(['-', '{', '}'], '', $namespace);
  20. // Binary Value
  21. $nstr = '';
  22. // Convert Namespace UUID to bits
  23. for ($i = 0; $i < strlen($nhex); $i += 2) {
  24. $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
  25. }
  26. // Calculate hash value
  27. $hash = md5($nstr . $name);
  28. return sprintf('%08s-%04s-%04x-%04x-%12s',
  29. // 32 bits for "time_low"
  30. substr($hash, 0, 8),
  31. // 16 bits for "time_mid"
  32. substr($hash, 8, 4),
  33. // 16 bits for "time_hi_and_version",
  34. // four most significant bits holds version number 3
  35. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  36. // 16 bits, 8 bits for "clk_seq_hi_res",
  37. // 8 bits for "clk_seq_low",
  38. // two most significant bits holds zero and one for variant DCE1.1
  39. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  40. // 48 bits for "node"
  41. substr($hash, 20, 12)
  42. );
  43. }
  44. /**
  45. * @return string
  46. */
  47. public static function v4()
  48. {
  49. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  50. // 32 bits for "time_low"
  51. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  52. // 16 bits for "time_mid"
  53. mt_rand(0, 0xffff),
  54. // 16 bits for "time_hi_and_version",
  55. // four most significant bits holds version number 4
  56. mt_rand(0, 0x0fff) | 0x4000,
  57. // 16 bits, 8 bits for "clk_seq_hi_res",
  58. // 8 bits for "clk_seq_low",
  59. // two most significant bits holds zero and one for variant DCE1.1
  60. mt_rand(0, 0x3fff) | 0x8000,
  61. // 48 bits for "node"
  62. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  63. );
  64. }
  65. /**
  66. * @param $namespace
  67. * @param $name
  68. * @return bool|string
  69. */
  70. public static function v5($namespace, $name)
  71. {
  72. if (!self::is_valid($namespace)) return false;
  73. // Get hexadecimal components of namespace
  74. $nhex = str_replace(['-', '{', '}'], '', $namespace);
  75. // Binary Value
  76. $nstr = '';
  77. // Convert Namespace UUID to bits
  78. for ($i = 0; $i < strlen($nhex); $i += 2) {
  79. $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
  80. }
  81. // Calculate hash value
  82. $hash = sha1($nstr . $name);
  83. return sprintf('%08s-%04s-%04x-%04x-%12s',
  84. // 32 bits for "time_low"
  85. substr($hash, 0, 8),
  86. // 16 bits for "time_mid"
  87. substr($hash, 8, 4),
  88. // 16 bits for "time_hi_and_version",
  89. // four most significant bits holds version number 5
  90. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  91. // 16 bits, 8 bits for "clk_seq_hi_res",
  92. // 8 bits for "clk_seq_low",
  93. // two most significant bits holds zero and one for variant DCE1.1
  94. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  95. // 48 bits for "node"
  96. substr($hash, 20, 12)
  97. );
  98. }
  99. /**
  100. * @param $uuid
  101. * @return bool
  102. */
  103. public static function is_valid($uuid)
  104. {
  105. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
  106. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  107. }
  108. }