URandom.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 URandom Random Number Source
  12. *
  13. * This uses the *nix /dev/urandom device to generate medium strength numbers
  14. *
  15. * PHP version 5.3
  16. *
  17. * @category PHPCryptLib
  18. * @package Random
  19. * @subpackage Source
  20. *
  21. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  22. * @author Paragon Initiative Enterprises <security@paragonie.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. /**
  31. * The URandom Random Number Source
  32. *
  33. * This uses the *nix /dev/urandom device to generate medium strength numbers
  34. *
  35. * @category PHPCryptLib
  36. * @package Random
  37. * @subpackage Source
  38. *
  39. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  40. * @author Paragon Initiative Enterprises <security@paragonie.com>
  41. * @codeCoverageIgnore
  42. */
  43. class URandom extends \RandomLib\AbstractSource
  44. {
  45. /**
  46. * @var string The file to read from
  47. */
  48. protected static $file = '/dev/urandom';
  49. /**
  50. * Return an instance of Strength indicating the strength of the source
  51. *
  52. * @return \SecurityLib\Strength An instance of one of the strength classes
  53. */
  54. public static function getStrength()
  55. {
  56. return new Strength(Strength::HIGH);
  57. }
  58. /**
  59. * If the source is currently available.
  60. * Reasons might be because the library is not installed
  61. *
  62. * @return bool
  63. */
  64. public static function isSupported()
  65. {
  66. return (bool) @\file_exists(static::$file);
  67. }
  68. /**
  69. * Generate a random string of the specified size
  70. *
  71. * @param int $size The size of the requested random string
  72. *
  73. * @return string A string of the requested size
  74. */
  75. public function generate($size)
  76. {
  77. if ($size == 0) {
  78. return static::emptyValue($size);
  79. }
  80. $file = \fopen(static::$file, 'rb');
  81. if (!\is_resource($file)) {
  82. /** @var string $result */
  83. $result = static::emptyValue($size);
  84. return $result;
  85. }
  86. if (\is_callable('stream_set_read_buffer')) {
  87. \stream_set_read_buffer($file, 0);
  88. }
  89. /** @var string $result */
  90. $result = \fread($file, $size);
  91. if (!\is_string($result)) {
  92. /** @var string $result */
  93. $result = static::emptyValue($size);
  94. return $result;
  95. }
  96. \fclose($file);
  97. return $result;
  98. }
  99. }