Sodium.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 libsodium Random Number Source
  12. *
  13. * This uses the libsodium secure generator to generate high 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 Ben Ramsey <ben@benramsey.com>
  23. * @author Paragon Initiative Enterprises <security@paragonie.com>
  24. * @copyright 2011 The Authors
  25. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  26. *
  27. * @version Build @@version@@
  28. *
  29. * @link https://paragonie.com/book/pecl-libsodium
  30. * @link http://pecl.php.net/package/libsodium
  31. */
  32. namespace RandomLib\Source;
  33. use SecurityLib\Strength;
  34. /**
  35. * The libsodium Random Number Source
  36. *
  37. * This uses the libsodium secure generator to generate high strength numbers
  38. *
  39. * @category PHPCryptLib
  40. * @package Random
  41. * @subpackage Source
  42. *
  43. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  44. * @author Ben Ramsey <ben@benramsey.com>
  45. * @author Paragon Initiative Enterprises <security@paragonie.com>
  46. */
  47. class Sodium extends \RandomLib\AbstractSource
  48. {
  49. /**
  50. * A property that may be forcibly set to `false` in the constructor, for
  51. * the purpose of testing this source
  52. *
  53. * @var bool
  54. */
  55. private $hasLibsodium = false;
  56. /**
  57. * Constructs a libsodium Random Number Source
  58. *
  59. * @param bool $useLibsodium May be set to `false` to disable libsodium for
  60. * testing purposes
  61. */
  62. public function __construct($useLibsodium = true)
  63. {
  64. if ($useLibsodium && extension_loaded('libsodium')) {
  65. $this->hasLibsodium = true;
  66. }
  67. }
  68. /**
  69. * If the source is currently available.
  70. * Reasons might be because the library is not installed
  71. *
  72. * @return bool
  73. */
  74. public static function isSupported()
  75. {
  76. return function_exists('Sodium\\randombytes_buf');
  77. }
  78. /**
  79. * Return an instance of Strength indicating the strength of the source
  80. *
  81. * @return Strength An instance of one of the strength classes
  82. */
  83. public static function getStrength()
  84. {
  85. return new Strength(Strength::HIGH);
  86. }
  87. /**
  88. * Generate a random string of the specified size
  89. *
  90. * @param int $size The size of the requested random string
  91. *
  92. * @return string A string of the requested size
  93. */
  94. public function generate($size)
  95. {
  96. if (!$this->hasLibsodium || $size < 1) {
  97. return str_repeat(chr(0), $size);
  98. }
  99. return (string) \Sodium\randombytes_buf($size);
  100. }
  101. }