PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/CryptLib/Random/Source/Rand.php

http://github.com/ircmaxell/PHP-CryptLib
PHP | 68 lines | 19 code | 6 blank | 43 comment | 3 complexity | c29af071feb08c0b6784aeb1826125c1 MD5 | raw file
  1. <?php
  2. /**
  3. * The Rand Random Number Source
  4. *
  5. * This source generates low strength random numbers by using the internal
  6. * rand() function. By itself it is quite weak. However when combined with
  7. * other sources it does provide significant benefit.
  8. *
  9. * PHP version 5.3
  10. *
  11. * @category PHPCryptLib
  12. * @package Random
  13. * @subpackage Source
  14. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  15. * @copyright 2011 The Authors
  16. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  17. * @version Build @@version@@
  18. */
  19. namespace CryptLib\Random\Source;
  20. use CryptLib\Core\Strength;
  21. /**
  22. * The Rand Random Number Source
  23. *
  24. * This source generates low strength random numbers by using the internal
  25. * rand() function. By itself it is quite weak. However when combined with
  26. * other sources it does provide significant benefit.
  27. *
  28. * @category PHPCryptLib
  29. * @package Random
  30. * @subpackage Source
  31. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  32. * @codeCoverageIgnore
  33. */
  34. class Rand implements \CryptLib\Random\Source {
  35. /**
  36. * Return an instance of Strength indicating the strength of the source
  37. *
  38. * @return Strength An instance of one of the strength classes
  39. */
  40. public static function getStrength() {
  41. // Detect if Suhosin Hardened PHP patch is applied
  42. if (defined('S_ALL')) {
  43. return new Strength(Strength::LOW);
  44. } else {
  45. return new Strength(Strength::VERYLOW);
  46. }
  47. }
  48. /**
  49. * Generate a random string of the specified size
  50. *
  51. * @param int $size The size of the requested random string
  52. *
  53. * @return string A string of the requested size
  54. */
  55. public function generate($size) {
  56. $result = '';
  57. for ($i = 0; $i < $size; $i++) {
  58. $result .= chr((rand() ^ rand()) % 256);
  59. }
  60. return $result;
  61. }
  62. }