MCryptRNGProvider.php 719 B

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