blob: eb425778156a793c4a4076e3836134d9ed599777 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2namespace RobThree\Auth\Providers\Rng;
3
4class 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}