PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/code/UuidFactory.php

https://github.com/rajeevgandhis/silverstripe-tools
PHP | 116 lines | 48 code | 30 blank | 38 comment | 4 complexity | 02f712c61f8f70fdbbe7a837eb0caaff MD5 | raw file
  1. <?php
  2. /**
  3. * UUID generator taken pretty much verbatim from
  4. * http://www.php.net/manual/en/function.uniqid.php#94959
  5. * @author sergeim
  6. *
  7. */
  8. class SSTools_UuidFactory extends Object {
  9. public static function v3($namespace, $name) {
  10. if(!self::is_valid($namespace)) return false;
  11. // Get hexadecimal components of namespace
  12. $nhex = str_replace(array('-','{','}'), '', $namespace);
  13. // Binary Value
  14. $nstr = '';
  15. // Convert Namespace UUID to bits
  16. for($i = 0; $i < strlen($nhex); $i+=2) {
  17. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  18. }
  19. // Calculate hash value
  20. $hash = md5($nstr . $name);
  21. return sprintf('%08s-%04s-%04x-%04x-%12s',
  22. // 32 bits for "time_low"
  23. substr($hash, 0, 8),
  24. // 16 bits for "time_mid"
  25. substr($hash, 8, 4),
  26. // 16 bits for "time_hi_and_version",
  27. // four most significant bits holds version number 3
  28. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  29. // 16 bits, 8 bits for "clk_seq_hi_res",
  30. // 8 bits for "clk_seq_low",
  31. // two most significant bits holds zero and one for variant DCE1.1
  32. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  33. // 48 bits for "node"
  34. substr($hash, 20, 12)
  35. );
  36. }
  37. public static function v4() {
  38. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  39. // 32 bits for "time_low"
  40. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  41. // 16 bits for "time_mid"
  42. mt_rand(0, 0xffff),
  43. // 16 bits for "time_hi_and_version",
  44. // four most significant bits holds version number 4
  45. mt_rand(0, 0x0fff) | 0x4000,
  46. // 16 bits, 8 bits for "clk_seq_hi_res",
  47. // 8 bits for "clk_seq_low",
  48. // two most significant bits holds zero and one for variant DCE1.1
  49. mt_rand(0, 0x3fff) | 0x8000,
  50. // 48 bits for "node"
  51. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  52. );
  53. }
  54. public static function v5($namespace, $name) {
  55. if(!self::is_valid($namespace)) return false;
  56. // Get hexadecimal components of namespace
  57. $nhex = str_replace(array('-','{','}'), '', $namespace);
  58. // Binary Value
  59. $nstr = '';
  60. // Convert Namespace UUID to bits
  61. for($i = 0; $i < strlen($nhex); $i+=2) {
  62. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  63. }
  64. // Calculate hash value
  65. $hash = sha1($nstr . $name);
  66. return sprintf('%08s-%04s-%04x-%04x-%12s',
  67. // 32 bits for "time_low"
  68. substr($hash, 0, 8),
  69. // 16 bits for "time_mid"
  70. substr($hash, 8, 4),
  71. // 16 bits for "time_hi_and_version",
  72. // four most significant bits holds version number 5
  73. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  74. // 16 bits, 8 bits for "clk_seq_hi_res",
  75. // 8 bits for "clk_seq_low",
  76. // two most significant bits holds zero and one for variant DCE1.1
  77. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  78. // 48 bits for "node"
  79. substr($hash, 20, 12)
  80. );
  81. }
  82. public static function is_valid($uuid) {
  83. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
  84. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  85. }
  86. }