PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/helpers/uuid_helper.php

https://github.com/kevinsmith/codeigniter-aws-fps
PHP | 159 lines | 62 code | 36 blank | 61 comment | 5 complexity | fd2dbc18e34d78a5041397067e9cd95f MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. * The following class generates VALID RFC 4211 COMPLIANT Universally Unique IDentifiers (UUID) version 3, 4 and 5.
  4. * Version 3 and 5 UUIDs are named based. They require a namespace (another valid UUID) and a value (the name). Given
  5. * the same namespace and name, the output is always the same.
  6. * Version 4 UUIDs are pseudo-random.
  7. * UUIDs generated below validates using OSSP UUID Tool, and output for named-based UUIDs are exactly the same.
  8. * This is a pure PHP implementation.
  9. *
  10. *
  11. * Usage
  12. * Named-based UUID.
  13. *
  14. * $v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  15. * $v5uuid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  16. *
  17. * Pseudo-random UUID
  18. *
  19. * $v4uuid = UUID::v4();
  20. */
  21. class UUID
  22. {
  23. public static function v3($namespace, $name)
  24. {
  25. if(!self::is_valid($namespace)) return false;
  26. // Get hexadecimal components of namespace
  27. $nhex = str_replace(array('-','{','}'), '', $namespace);
  28. // Binary Value
  29. $nstr = '';
  30. // Convert Namespace UUID to bits
  31. for($i = 0; $i < strlen($nhex); $i+=2) {
  32. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  33. }
  34. // Calculate hash value
  35. $hash = md5($nstr . $name);
  36. return sprintf('%08s-%04s-%04x-%04x-%12s',
  37. // 32 bits for "time_low"
  38. substr($hash, 0, 8),
  39. // 16 bits for "time_mid"
  40. substr($hash, 8, 4),
  41. // 16 bits for "time_hi_and_version",
  42. // four most significant bits holds version number 3
  43. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  44. // 16 bits, 8 bits for "clk_seq_hi_res",
  45. // 8 bits for "clk_seq_low",
  46. // two most significant bits holds zero and one for variant DCE1.1
  47. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  48. // 48 bits for "node"
  49. substr($hash, 20, 12)
  50. );
  51. }
  52. public static function v4()
  53. {
  54. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  55. // return sprintf('%04x%04x%04x%04x%04x%04x%04x%04x',
  56. // 32 bits for "time_low"
  57. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  58. // 16 bits for "time_mid"
  59. mt_rand(0, 0xffff),
  60. // 16 bits for "time_hi_and_version",
  61. // four most significant bits holds version number 4
  62. mt_rand(0, 0x0fff) | 0x4000,
  63. // 16 bits, 8 bits for "clk_seq_hi_res",
  64. // 8 bits for "clk_seq_low",
  65. // two most significant bits holds zero and one for variant DCE1.1
  66. mt_rand(0, 0x3fff) | 0x8000,
  67. // 48 bits for "node"
  68. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  69. );
  70. }
  71. public static function GUID()
  72. {
  73. return strtoupper(sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  74. // return sprintf('%04x%04x%04x%04x%04x%04x%04x%04x',
  75. // 32 bits for "time_low"
  76. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  77. // 16 bits for "time_mid"
  78. mt_rand(0, 0xffff),
  79. // 16 bits for "time_hi_and_version",
  80. // four most significant bits holds version number 4
  81. mt_rand(0, 0x0fff) | 0x4000,
  82. // 16 bits, 8 bits for "clk_seq_hi_res",
  83. // 8 bits for "clk_seq_low",
  84. // two most significant bits holds zero and one for variant DCE1.1
  85. mt_rand(0, 0x3fff) | 0x8000,
  86. // 48 bits for "node"
  87. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  88. ));
  89. }
  90. public static function v5($namespace, $name)
  91. {
  92. if(!self::is_valid($namespace)) return false;
  93. // Get hexadecimal components of namespace
  94. $nhex = str_replace(array('-','{','}'), '', $namespace);
  95. // Binary Value
  96. $nstr = '';
  97. // Convert Namespace UUID to bits
  98. for($i = 0; $i < strlen($nhex); $i+=2) {
  99. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  100. }
  101. // Calculate hash value
  102. $hash = sha1($nstr . $name);
  103. return sprintf('%08s-%04s-%04x-%04x-%12s',
  104. // 32 bits for "time_low"
  105. substr($hash, 0, 8),
  106. // 16 bits for "time_mid"
  107. substr($hash, 8, 4),
  108. // 16 bits for "time_hi_and_version",
  109. // four most significant bits holds version number 5
  110. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  111. // 16 bits, 8 bits for "clk_seq_hi_res",
  112. // 8 bits for "clk_seq_low",
  113. // two most significant bits holds zero and one for variant DCE1.1
  114. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  115. // 48 bits for "node"
  116. substr($hash, 20, 12)
  117. );
  118. }
  119. public static function is_valid($uuid)
  120. {
  121. 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;
  122. }
  123. }