1 | <?php |
||
19 | class SecureRandom |
||
20 | { |
||
21 | /** @var Generator The secure random byte generator used to generate bytes */ |
||
22 | private $generator; |
||
23 | |||
24 | /** @var string[] List of default generators */ |
||
25 | private static $defaultGenerators = [ |
||
26 | '\Riimu\Kit\SecureRandom\Generator\Internal', |
||
27 | '\Riimu\Kit\SecureRandom\Generator\RandomReader', |
||
28 | '\Riimu\Kit\SecureRandom\Generator\Mcrypt', |
||
29 | '\Riimu\Kit\SecureRandom\Generator\OpenSSL', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * Creates a new instance of SecureRandom. |
||
34 | * |
||
35 | * You can either provide a generator to use for generating random bytes or |
||
36 | * give null as the argument to use default generators. If null is provided, |
||
37 | * the constructor will attempt to create the random byte generators in the |
||
38 | * following order until it finds one that is supported: |
||
39 | * |
||
40 | * - Internal |
||
41 | * - RandomReader |
||
42 | * - Mcrypt |
||
43 | * - OpenSSL |
||
44 | * |
||
45 | * Note that since most cases require non-blocking random generation, the |
||
46 | * default generators use /dev/urandom as the random source. If you do not |
||
47 | * think this provides enough security, create the desired random generator |
||
48 | * using /dev/random as the source. |
||
49 | * |
||
50 | * @param Generator|null $generator Random byte generator or null for default |
||
51 | * @throws GeneratorException If the provided or default generators are not supported |
||
52 | */ |
||
53 | 31 | public function __construct(Generator $generator = null) |
|
63 | |||
64 | /** |
||
65 | * Returns the first supported default secure random byte generator. |
||
66 | * @return Generator Supported secure random byte generator |
||
67 | * @throws GeneratorException If none of the default generators are supported |
||
68 | */ |
||
69 | 2 | private function getDefaultGenerator() |
|
81 | |||
82 | /** |
||
83 | * Returns a number of random bytes. |
||
84 | * @param int $count Number of random bytes to return |
||
85 | * @return string Randomly generated bytes |
||
86 | * @throws \InvalidArgumentException If the count is invalid |
||
87 | */ |
||
88 | 3 | public function getBytes($count) |
|
100 | |||
101 | /** |
||
102 | * Returns a random number between 0 and the limit. |
||
103 | * @param int $limit Maximum random number |
||
104 | * @return int Random number between 0 and the limit |
||
105 | */ |
||
106 | 13 | private function getNumber($limit) |
|
116 | |||
117 | /** |
||
118 | * Returns a random number generated using the random byte generator. |
||
119 | * @param int $limit Maximum value for the random number |
||
120 | * @return int The generated random number between 0 and the limit |
||
121 | */ |
||
122 | 8 | private function getByteNumber($limit) |
|
136 | |||
137 | /** |
||
138 | * Returns a random integer between two positive integers (inclusive). |
||
139 | * @param int $min Minimum limit |
||
140 | * @param int $max Maximum limit |
||
141 | * @return int Random integer between minimum and maximum limit |
||
142 | * @throws \InvalidArgumentException If the limits are invalid |
||
143 | */ |
||
144 | 8 | public function getInteger($min, $max) |
|
155 | |||
156 | /** |
||
157 | * Returns a random float between 0 and 1 (excluding the number 1). |
||
158 | * @return float Random float between 0 and 1 (excluding 1) |
||
|
|||
159 | */ |
||
160 | public function getRandom() |
||
172 | |||
173 | /** |
||
174 | * Returns a random float between 0 and 1 (inclusive). |
||
175 | * @return float Random float between 0 and 1 (inclusive) |
||
176 | */ |
||
177 | 1 | public function getFloat() |
|
181 | |||
182 | /** |
||
183 | * Returns a number of randomly selected elements from the array. |
||
184 | * |
||
185 | * This method returns randomly selected elements from the array. The number |
||
186 | * of elements is determined by by the second argument. The elements are |
||
187 | * returned in random order but the keys are preserved. |
||
188 | * |
||
189 | * @param array $array Array of elements |
||
190 | * @param int $count Number of elements to return from the array |
||
191 | * @return array Randomly selected elements in random order |
||
192 | * @throws \InvalidArgumentException If the count is invalid |
||
193 | */ |
||
194 | 7 | public function getArray(array $array, $count) |
|
217 | |||
218 | /** |
||
219 | * Returns one randomly selected value from the array. |
||
220 | * @param array $array The array to choose from |
||
221 | * @return mixed One randomly selected value from the array |
||
222 | * @throws \InvalidArgumentException If the array is empty |
||
223 | */ |
||
224 | 3 | public function choose(array $array) |
|
232 | |||
233 | /** |
||
234 | * Returns the array with the elements reordered in a random order. |
||
235 | * @param array $array The array to shuffle |
||
236 | * @return array The provided array with elements in a random order |
||
237 | */ |
||
238 | 2 | public function shuffle(array $array) |
|
242 | |||
243 | /** |
||
244 | * Returns a random sequence of values. |
||
245 | * |
||
246 | * If a string is provided as the first argument, the method returns a |
||
247 | * string with characters selected from the provided string. The length of |
||
248 | * the returned string is determined by the second argument. |
||
249 | * |
||
250 | * If an array is provided as the first argument, the method returns an |
||
251 | * array with elements selected from the provided array. The size of the |
||
252 | * returned array is determined by the second argument. |
||
253 | * |
||
254 | * The functionality is similar to getArray(), except for the fact that the |
||
255 | * returned value can contain the same character or element multiple times. |
||
256 | * If the same character or element appears multiple times in the provided |
||
257 | * argument, it will increase the relative chance of it appearing in the |
||
258 | * returned value. |
||
259 | * |
||
260 | * @param string|array $choices Values to choose from |
||
261 | * @param int $length Length of the sequence |
||
262 | * @return array|string The generated random sequence |
||
263 | * @throws \InvalidArgumentException If the choices or length is invalid |
||
264 | */ |
||
265 | 6 | public function getSequence($choices, $length) |
|
279 | |||
280 | /** |
||
281 | * Returns the selected list of values for the sequence. |
||
282 | * @param array $values List of possible values |
||
283 | * @param int $length Number of values to return |
||
284 | * @return array Selected list of values for the sequence |
||
285 | */ |
||
286 | 5 | private function getSequenceValues(array $values, $length) |
|
303 | |||
304 | /** |
||
305 | * Returns a random UUID version 4 identifier. |
||
306 | * @return string A random UUID identifier |
||
307 | */ |
||
308 | public function getUuid() |
||
319 | } |
||
320 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.