Files
TwoFactorAuth/lib/Providers/Rng/HashRNGProvider.php
T
RobThree 0ac68f6b86 * Fix PHP7.2 issue with HashRNGProvider
* Fix PHP7.1+ issue with MCryptRNGProvider
2018-06-06 01:47:03 +02:00

29 lines
785 B
PHP

<?php
namespace RobThree\Auth\Providers\Rng;
class HashRNGProvider implements IRNGProvider
{
private $algorithm;
function __construct($algorithm = 'sha256' ) {
$algos = array_values(hash_algos());
if (!in_array($algorithm, $algos, true))
throw new \RNGException('Unsupported algorithm specified');
$this->algorithm = $algorithm;
}
public function getRandomBytes($bytecount) {
$result = '';
$hash = mt_rand();
for ($i = 0; $i < $bytecount; $i++) {
$hash = hash($this->algorithm, $hash.mt_rand(), true);
$result .= $hash[mt_rand(0, strlen($hash)-1)];
}
return $result;
}
public function isCryptographicallySecure() {
return false;
}
}