HashRNGProvider.php 952 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace RobThree\Auth\Providers\Rng;
  3. class HashRNGProvider implements IRNGProvider
  4. {
  5. /** @var string */
  6. private $algorithm;
  7. /**
  8. * @param string $algorithm
  9. */
  10. public function __construct($algorithm = 'sha256')
  11. {
  12. $algos = array_values(hash_algos());
  13. if (!in_array($algorithm, $algos, true)) {
  14. throw new RNGException('Unsupported algorithm specified');
  15. }
  16. $this->algorithm = $algorithm;
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getRandomBytes($bytecount)
  22. {
  23. $result = '';
  24. $hash = mt_rand();
  25. for ($i = 0; $i < $bytecount; $i++) {
  26. $hash = hash($this->algorithm, $hash . mt_rand(), true);
  27. $result .= $hash[mt_rand(0, strlen($hash) - 1)];
  28. }
  29. return $result;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function isCryptographicallySecure()
  35. {
  36. return false;
  37. }
  38. }