OpenSSLRNGProvider.php 968 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace RobThree\Auth\Providers\Rng;
  3. class OpenSSLRNGProvider implements IRNGProvider
  4. {
  5. /** @var bool */
  6. private $requirestrong;
  7. /**
  8. * @param bool $requirestrong
  9. */
  10. public function __construct($requirestrong = true)
  11. {
  12. $this->requirestrong = $requirestrong;
  13. }
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function getRandomBytes($bytecount)
  18. {
  19. $result = openssl_random_pseudo_bytes($bytecount, $crypto_strong);
  20. if ($this->requirestrong && ($crypto_strong === false)) {
  21. throw new RNGException('openssl_random_pseudo_bytes returned non-cryptographically strong value');
  22. }
  23. if ($result === false) {
  24. throw new RNGException('openssl_random_pseudo_bytes returned an invalid value');
  25. }
  26. return $result;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function isCryptographicallySecure()
  32. {
  33. return $this->requirestrong;
  34. }
  35. }