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