HashRNGProvider.php 785 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. namespace RobThree\Auth\Providers\Rng;
  3. class HashRNGProvider implements IRNGProvider
  4. {
  5. private $algorithm;
  6. function __construct($algorithm = 'sha256' ) {
  7. $algos = array_values(hash_algos());
  8. if (!in_array($algorithm, $algos, true))
  9. throw new \RNGException('Unsupported algorithm specified');
  10. $this->algorithm = $algorithm;
  11. }
  12. public function getRandomBytes($bytecount) {
  13. $result = '';
  14. $hash = mt_rand();
  15. for ($i = 0; $i < $bytecount; $i++) {
  16. $hash = hash($this->algorithm, $hash.mt_rand(), true);
  17. $result .= $hash[mt_rand(0, strlen($hash)-1)];
  18. }
  19. return $result;
  20. }
  21. public function isCryptographicallySecure() {
  22. return false;
  23. }
  24. }