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

/library/Daz/Crypt/Guid.php

https://github.com/dlorenso/dazlo
PHP | 88 lines | 36 code | 3 blank | 49 comment | 0 complexity | 88599d4cd9c0e95de0724e98de872b10 MD5 | raw file
  1. <?php
  2. /**
  3. * Dazlo Framework Copyright (c) 2012 D. Dante Lorenso. All Rights Reserved.
  4. * This source file is subject to the new BSD license that is bundled with
  5. * this package in the file LICENSE.txt. It is also available through the
  6. * world-wide web at this URL:
  7. * http://www.opensource.org/licenses/bsd-license.php
  8. */
  9. namespace Daz\Crypt;
  10. class Guid
  11. {
  12. /**
  13. * Like "uuid" function, but in a more readable format, and without hyphens.
  14. * @return string A UUID, made up of 32 hex digits.
  15. */
  16. public static function guid()
  17. {
  18. return call_user_func_array(
  19. 'sprintf',
  20. array(
  21. // format
  22. '%04x%04x' . '%04x' . '%03x4' . '%04x' . '%04x%04x%04x',
  23. // 32 bits for "time_low"
  24. mt_rand(0, 65535),
  25. mt_rand(0, 65535),
  26. // 16 bits for "time_mid"
  27. mt_rand(0, 65535),
  28. // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
  29. mt_rand(0, 4095),
  30. // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
  31. // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
  32. // 8 bits for "clk_seq_low"
  33. bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
  34. // 48 bits for "node"
  35. mt_rand(0, 65535),
  36. mt_rand(0, 65535),
  37. mt_rand(0, 65535)
  38. )
  39. );
  40. }
  41. /**
  42. * Generates a Universally Unique IDentifier, version 4.
  43. * RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt) defines a special type of
  44. * Globally Unique IDentifiers (GUID), as well as several methods for
  45. * producing them. One such method, described in section 4.4, is based on
  46. * truly random or pseudo-random number generators, and is therefore
  47. * implementable in a language like PHP.
  48. * We choose to produce pseudo-random numbers with the Mersenne Twister, and
  49. * to always limit single generated numbers to 16 bits (ie. the decimal
  50. * value 65535). That is because, even on 32-bit systems, PHP's RAND_MAX
  51. * will often be the maximum *signed* value, with only the equivalent of 31
  52. * significant bits. Producing two 16-bit random numbers to make up a 32-bit
  53. * one is less efficient, but guarantees that all 32 bits are random.
  54. * The algorithm for version 4 UUIDs (ie. those based on random number
  55. * generators) states that all 128 bits separated into the various fields
  56. * (32 bits, 16 bits, 16 bits, 8 bits and 8 bits, 48 bits) should be random,
  57. * except : (a) the version number should be the last 4 bits in the 3rd
  58. * field, and (b) bits 6 and 7 of the 4th field should be 01. We try to
  59. * conform to that definition as efficiently as possible, generating smaller
  60. * values where possible, and minimizing the number of base conversions.
  61. * @copyright Copyright (c) CFD Labs, 2006. This function may be used freely
  62. * for any purpose ; it is distributed without any form of warranty
  63. * whatsoever.
  64. * @author David Holmes <dholmes@cfdsoftware.net>
  65. * @return string A UUID, made up of 32 hex digits and 4 hyphens.
  66. */
  67. public static function uuid()
  68. {
  69. // The field names refer to RFC 4122 section 4.1.2
  70. return sprintf(
  71. '%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
  72. mt_rand(0, 65535),
  73. mt_rand(0, 65535), // 32 bits for "time_low"
  74. mt_rand(0, 65535), // 16 bits for "time_mid"
  75. mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
  76. bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
  77. // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
  78. // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
  79. // 8 bits for "clk_seq_low"
  80. mt_rand(0, 65535),
  81. mt_rand(0, 65535),
  82. mt_rand(0, 65535) // 48 bits for "node"
  83. );
  84. }
  85. }