PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helpers/classes/uuid.php

https://github.com/mmarinero/ProjectManagementExample
PHP | 159 lines | 52 code | 38 blank | 69 comment | 4 complexity | e679f908ec5b8045b15dd0815c854c33 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * UUID generator class
  4. *
  5. * Generates valid RFC 4211 compliant Universally Unique IDentifiers (UUID) version 3, 4 and 5.
  6. * UUIDs generated validate using the OSSP UUID Tool, and the output for named-based UUIDs are
  7. * exactly the same. This is a pure PHP implementation.
  8. *
  9. * Usage:
  10. *
  11. * Name-based UUID:
  12. *
  13. * $v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
  14. * $v5uuid = UUID::v5(UUID::NS_URL, 'http://www.google.com/');
  15. *
  16. * Pseudo-random UUID:
  17. *
  18. * $v4uuid = UUID::v4();
  19. *
  20. *
  21. * Originally found at: http://www.php.net/manual/en/function.uniqid.php#94959
  22. *
  23. * @author Andrew Moore
  24. *
  25. *
  26. * Modifications made by Henry Merriam <php@henrymerriam.com> on 2009-12-20:
  27. *
  28. * + Added constants for predefined namespaces as defined in RFC 4211 Appendix C.
  29. * + NS_DNS
  30. * + NS_URL
  31. * + NS_ISO_UID
  32. * + NS_X500_DN
  33. *
  34. * + Wrote this documentation comment.
  35. *
  36. */
  37. class UUID {
  38. const NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // FQDN
  39. const NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; // URL
  40. const NS_ISO_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; // ISO OID
  41. const NS_X500_DN = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; // X.500 DN (in DER or a text output format)
  42. public static function v3($namespace, $name) {
  43. if(!self::is_valid($namespace)) return false;
  44. // Get hexadecimal components of namespace
  45. $nhex = str_replace(array('-','{','}'), '', $namespace);
  46. // Binary Value
  47. $nstr = '';
  48. // Convert Namespace UUID to bits
  49. for($i = 0; $i < strlen($nhex); $i+=2) {
  50. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  51. }
  52. // Calculate hash value
  53. $hash = md5($nstr . $name);
  54. // Format and return UUID
  55. return sprintf('%08s-%04s-%04x-%04x-%12s',
  56. // 32 bits for "time_low"
  57. substr($hash, 0, 8),
  58. // 16 bits for "time_mid"
  59. substr($hash, 8, 4),
  60. // 16 bits for "time_hi_and_version",
  61. // four most significant bits holds version number 3
  62. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  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. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  67. // 48 bits for "node"
  68. substr($hash, 20, 12)
  69. );
  70. }
  71. public static function v4() {
  72. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  73. // 32 bits for "time_low"
  74. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  75. // 16 bits for "time_mid"
  76. mt_rand(0, 0xffff),
  77. // 16 bits for "time_hi_and_version",
  78. // four most significant bits holds version number 4
  79. mt_rand(0, 0x0fff) | 0x4000,
  80. // 16 bits, 8 bits for "clk_seq_hi_res",
  81. // 8 bits for "clk_seq_low",
  82. // two most significant bits holds zero and one for variant DCE1.1
  83. mt_rand(0, 0x3fff) | 0x8000,
  84. // 48 bits for "node"
  85. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  86. );
  87. }
  88. public static function v5($namespace, $name) {
  89. if(!self::is_valid($namespace)) return false;
  90. // Get hexadecimal components of namespace
  91. $nhex = str_replace(array('-','{','}'), '', $namespace);
  92. // Binary Value
  93. $nstr = '';
  94. // Convert Namespace UUID to bits
  95. for($i = 0; $i < strlen($nhex); $i+=2) {
  96. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  97. }
  98. // Calculate hash value
  99. $hash = sha1($nstr . $name);
  100. // Format and return UUID
  101. return sprintf('%08s-%04s-%04x-%04x-%12s',
  102. // 32 bits for "time_low"
  103. substr($hash, 0, 8),
  104. // 16 bits for "time_mid"
  105. substr($hash, 8, 4),
  106. // 16 bits for "time_hi_and_version",
  107. // four most significant bits holds version number 5
  108. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  109. // 16 bits, 8 bits for "clk_seq_hi_res",
  110. // 8 bits for "clk_seq_low",
  111. // two most significant bits holds zero and one for variant DCE1.1
  112. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  113. // 48 bits for "node"
  114. substr($hash, 20, 12)
  115. );
  116. }
  117. public static function is_valid($uuid) {
  118. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
  119. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  120. }
  121. }