PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/System/Library/UUID.php

https://github.com/fachri/MFW
PHP | 99 lines | 51 code | 16 blank | 32 comment | 5 complexity | adb974626b251966684ac735a82eb140 MD5 | raw file
  1. <?php if (! defined('SYSTEM')) exit('No direct script access allowed');
  2. class UUID {
  3. public function V3($namespace, $name) {
  4. if( ! $this->IsValid($namespace))
  5. return false;
  6. // Get hexadecimal components of namespace
  7. $nhex = str_replace(array('-','{','}'), '', $namespace);
  8. // Binary Value
  9. $nstr = '';
  10. // Convert Namespace UUID to bits
  11. for($i = 0; $i < strlen($nhex); $i+=2)
  12. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  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 function V4()
  32. {
  33. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  34. // 32 bits for "time_low"
  35. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  36. // 16 bits for "time_mid"
  37. mt_rand(0, 0xffff),
  38. // 16 bits for "time_hi_and_version",
  39. // four most significant bits holds version number 4
  40. mt_rand(0, 0x0fff) | 0x4000,
  41. // 16 bits, 8 bits for "clk_seq_hi_res",
  42. // 8 bits for "clk_seq_low",
  43. // two most significant bits holds zero and one for variant DCE1.1
  44. mt_rand(0, 0x3fff) | 0x8000,
  45. // 48 bits for "node"
  46. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  47. );
  48. }
  49. public function V5($namespace, $name)
  50. {
  51. if( ! $this->IsValid($namespace))
  52. return false;
  53. // Get hexadecimal components of namespace
  54. $nhex = str_replace(array('-','{','}'), '', $namespace);
  55. // Binary Value
  56. $nstr = '';
  57. // Convert Namespace UUID to bits
  58. for($i = 0; $i < strlen($nhex); $i+=2)
  59. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  60. // Calculate hash value
  61. $hash = sha1($nstr . $name);
  62. return sprintf('%08s-%04s-%04x-%04x-%12s',
  63. // 32 bits for "time_low"
  64. substr($hash, 0, 8),
  65. // 16 bits for "time_mid"
  66. substr($hash, 8, 4),
  67. // 16 bits for "time_hi_and_version",
  68. // four most significant bits holds version number 5
  69. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  70. // 16 bits, 8 bits for "clk_seq_hi_res",
  71. // 8 bits for "clk_seq_low",
  72. // two most significant bits holds zero and one for variant DCE1.1
  73. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  74. // 48 bits for "node"
  75. substr($hash, 20, 12)
  76. );
  77. }
  78. private function IsValid($uuid)
  79. {
  80. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  81. }
  82. }
  83. ?>