1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riimu\Kit\SecureRandom\Generator; |
4
|
|
|
|
5
|
|
|
use Riimu\Kit\SecureRandom\GeneratorException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Generates bytes and numbers using PHP's built in CSPRNG. |
9
|
|
|
* |
10
|
|
|
* PHP7 offers a built in function for generating cryptographically secure |
11
|
|
|
* random bytes. This class simply wraps that method for supported PHP versions. |
12
|
|
|
* |
13
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
14
|
|
|
* @copyright Copyright (c) 2014, Riikka Kalliomäki |
15
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
16
|
|
|
*/ |
17
|
|
|
class Internal extends AbstractGenerator implements NumberGenerator |
18
|
|
|
{ |
19
|
7 |
|
public function isSupported() |
20
|
|
|
{ |
21
|
7 |
|
return version_compare(PHP_VERSION, '7.0', '>='); |
22
|
|
|
} |
23
|
|
|
|
24
|
1 |
|
protected function readBytes($count) |
25
|
|
|
{ |
26
|
1 |
|
return random_bytes($count); |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function getNumber($min, $max) |
30
|
|
|
{ |
31
|
2 |
|
$min = (int) $min; |
32
|
2 |
|
$max = (int) $max; |
33
|
2 |
|
$exception = null; |
34
|
|
|
|
35
|
|
|
try { |
36
|
2 |
|
$number = random_int($min, $max); |
37
|
1 |
|
} catch (\Throwable $exception) { |
|
|
|
|
38
|
1 |
|
$number = false; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
if (!$this->isValidResult($number, $min, $max)) { |
42
|
1 |
|
throw new GeneratorException('Error generating random number', 0, $exception); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
return $number; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Tells if the generated number is a valid result. |
50
|
|
|
* @param int $number The number to test |
51
|
|
|
* @param int $min The minimum value for the number |
52
|
|
|
* @param int $max The maximum value for the number |
53
|
|
|
* @return bool True if the number is a valid result, false if not |
54
|
|
|
*/ |
55
|
2 |
|
private function isValidResult($number, $min, $max) |
56
|
|
|
{ |
57
|
2 |
|
return is_int($number) && $number >= $min && $number <= $max; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.