Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace RobThree\Auth\Providers\Rng; |
| 4 | |
| 5 | class OpenSSLRNGProvider implements IRNGProvider |
| 6 | { |
| 7 | private $requirestrong; |
| 8 | |
| 9 | function __construct($requirestrong = true) { |
| 10 | $this->requirestrong = $requirestrong; |
| 11 | } |
| 12 | |
| 13 | public function getRandomBytes($bytecount) { |
| 14 | $result = openssl_random_pseudo_bytes($bytecount, $crypto_strong); |
| 15 | if ($this->requirestrong && ($crypto_strong === false)) |
| 16 | throw new \RNGException('openssl_random_pseudo_bytes returned non-cryptographically strong value'); |
| 17 | if ($result === false) |
| 18 | throw new \RNGException('openssl_random_pseudo_bytes returned an invalid value'); |
| 19 | return $result; |
| 20 | } |
| 21 | |
| 22 | public function isCryptographicallySecure() { |
| 23 | return $this->requirestrong; |
| 24 | } |
| 25 | } |