UniqID.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * The RandomLib library for securely generating random numbers and strings in PHP
  4. *
  5. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  6. * @copyright 2011 The Authors
  7. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  8. * @version Build @@version@@
  9. */
  10. /**
  11. * The UniqID Random Number Source
  12. *
  13. * This uses the internal `uniqid()` function to generate low strength random
  14. * numbers.
  15. *
  16. * PHP version 5.3
  17. *
  18. * @category PHPCryptLib
  19. * @package Random
  20. * @subpackage Source
  21. *
  22. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  23. * @copyright 2011 The Authors
  24. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  25. *
  26. * @version Build @@version@@
  27. */
  28. namespace RandomLib\Source;
  29. use SecurityLib\Strength;
  30. use SecurityLib\Util;
  31. /**
  32. * The UniqID Random Number Source
  33. *
  34. * This uses the internal `uniqid()` function to generate low strength random
  35. * numbers.
  36. *
  37. * @category PHPCryptLib
  38. * @package Random
  39. * @subpackage Source
  40. *
  41. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  42. * @codeCoverageIgnore
  43. */
  44. class UniqID extends \RandomLib\AbstractSource
  45. {
  46. /**
  47. * Return an instance of Strength indicating the strength of the source
  48. *
  49. * @return \SecurityLib\Strength An instance of one of the strength classes
  50. */
  51. public static function getStrength()
  52. {
  53. return new Strength(Strength::LOW);
  54. }
  55. /**
  56. * Generate a random string of the specified size
  57. *
  58. * @param int $size The size of the requested random string
  59. *
  60. * @return string A string of the requested size
  61. */
  62. public function generate($size)
  63. {
  64. $result = '';
  65. while (Util::safeStrlen($result) < $size) {
  66. $result = uniqid($result, true);
  67. }
  68. return Util::safeSubstr($result, 0, $size);
  69. }
  70. }