PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/RandomLib/Source/UniqID.php

https://github.com/ezimuel/RandomLib
PHP | 61 lines | 15 code | 6 blank | 40 comment | 1 complexity | d9366d9c71d4dbf868b92c1c2418b6b4 MD5 | raw file
  1. <?php
  2. /**
  3. * The UniqID Random Number Source
  4. *
  5. * This uses the internal `uniqid()` function to generate low strength random
  6. * numbers.
  7. *
  8. * PHP version 5.3
  9. *
  10. * @category PHPCryptLib
  11. * @package Random
  12. * @subpackage Source
  13. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  14. * @copyright 2011 The Authors
  15. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  16. * @version Build @@version@@
  17. */
  18. namespace RandomLib\Source;
  19. use SecurityLib\Strength;
  20. /**
  21. * The UniqID Random Number Source
  22. *
  23. * This uses the internal `uniqid()` function to generate low strength random
  24. * numbers.
  25. *
  26. * @category PHPCryptLib
  27. * @package Random
  28. * @subpackage Source
  29. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  30. * @codeCoverageIgnore
  31. */
  32. class UniqID implements \RandomLib\Source {
  33. /**
  34. * Return an instance of Strength indicating the strength of the source
  35. *
  36. * @return Strength An instance of one of the strength classes
  37. */
  38. public static function getStrength() {
  39. return new Strength(Strength::LOW);
  40. }
  41. /**
  42. * Generate a random string of the specified size
  43. *
  44. * @param int $size The size of the requested random string
  45. *
  46. * @return string A string of the requested size
  47. */
  48. public function generate($size) {
  49. $result = '';
  50. while (strlen($result) < $size) {
  51. $result = uniqid($result, true);
  52. }
  53. return substr($result, 0, $size);
  54. }
  55. }