1 | <?php |
||
19 | class SecureRandom |
||
20 | { |
||
21 | /** @var NumberGenerator The secure random generator used to generate bytes and numbers */ |
||
22 | private $generator; |
||
23 | |||
24 | /** @var string[] List of default generators */ |
||
25 | private static $defaultGenerators = [ |
||
26 | Generator\Internal::class, |
||
27 | Generator\RandomReader::class, |
||
28 | Generator\Mcrypt::class, |
||
29 | Generator\OpenSSL::class, |
||
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\Generator|null $generator Random byte generator or null for default |
||
51 | * @throws GeneratorException If the provided or default generators are not supported |
||
52 | */ |
||
53 | 98 | public function __construct(Generator\Generator $generator = null) |
|
67 | |||
68 | /** |
||
69 | * Returns the first supported default secure random byte generator. |
||
70 | * @return Generator\Generator Supported secure random byte generator |
||
71 | * @throws GeneratorException If none of the default generators are supported |
||
72 | */ |
||
73 | 6 | private function getDefaultGenerator() |
|
86 | |||
87 | /** |
||
88 | * Returns a number of random bytes. |
||
89 | * @param int $count Number of random bytes to return |
||
90 | * @return string Randomly generated bytes |
||
91 | * @throws \InvalidArgumentException If the count is invalid |
||
92 | */ |
||
93 | 9 | public function getBytes($count) |
|
103 | |||
104 | /** |
||
105 | * Returns a random integer between two positive integers (inclusive). |
||
106 | * @param int $min Minimum limit |
||
107 | * @param int $max Maximum limit |
||
108 | * @return int Random integer between minimum and maximum limit |
||
109 | * @throws \InvalidArgumentException If the limits are invalid |
||
110 | */ |
||
111 | 23 | public function getInteger($min, $max) |
|
122 | |||
123 | /** |
||
124 | * Tells if the given number is not within given limits (inclusive). |
||
125 | * @param int $number The number to test |
||
126 | * @param int $min The minimum allowed limit |
||
127 | * @param int $max The maximum allowed limit |
||
128 | * @return bool True if the number is out of bounds, false if within bounds |
||
129 | */ |
||
130 | 44 | private function isOutOfBounds($number, $min, $max) |
|
134 | |||
135 | /** |
||
136 | * Returns a random float between 0 and 1 (excluding the number 1). |
||
137 | * @return float Random float between 0 and 1 (excluding 1) |
||
138 | */ |
||
139 | 3 | public function getRandom() |
|
152 | |||
153 | /** |
||
154 | * Returns a random float between 0 and 1 (inclusive). |
||
155 | * @return float Random float between 0 and 1 (inclusive) |
||
156 | */ |
||
157 | 3 | public function getFloat() |
|
161 | |||
162 | /** |
||
163 | * Returns a number of randomly selected elements from the array. |
||
164 | * |
||
165 | * This method returns randomly selected elements from the array. The number |
||
166 | * of elements is determined by by the second argument. The elements are |
||
167 | * returned in random order but the keys are preserved. |
||
168 | * |
||
169 | * @param array $array Array of elements |
||
170 | * @param int $count Number of elements to return from the array |
||
171 | * @return array Randomly selected elements in random order |
||
172 | * @throws \InvalidArgumentException If the count is invalid |
||
173 | */ |
||
174 | 21 | public function getArray(array $array, $count) |
|
194 | |||
195 | /** |
||
196 | * Returns one randomly selected value from the array. |
||
197 | * @param array $array The array to choose from |
||
198 | * @return mixed One randomly selected value from the array |
||
199 | * @throws \InvalidArgumentException If the array is empty |
||
200 | */ |
||
201 | 9 | public function choose(array $array) |
|
211 | |||
212 | /** |
||
213 | * Returns the array with the elements reordered in a random order. |
||
214 | * @param array $array The array to shuffle |
||
215 | * @return array The provided array with elements in a random order |
||
216 | */ |
||
217 | 6 | public function shuffle(array $array) |
|
221 | |||
222 | /** |
||
223 | * Returns a random sequence of values. |
||
224 | * |
||
225 | * If a string is provided as the first argument, the method returns a |
||
226 | * string with characters selected from the provided string. The length of |
||
227 | * the returned string is determined by the second argument. |
||
228 | * |
||
229 | * If an array is provided as the first argument, the method returns an |
||
230 | * array with elements selected from the provided array. The size of the |
||
231 | * returned array is determined by the second argument. |
||
232 | * |
||
233 | * The functionality is similar to getArray(), except for the fact that the |
||
234 | * returned value can contain the same character or element multiple times. |
||
235 | * If the same character or element appears multiple times in the provided |
||
236 | * argument, it will increase the relative chance of it appearing in the |
||
237 | * returned value. |
||
238 | * |
||
239 | * @param string|array $choices Values to choose from |
||
240 | * @param int $length Length of the sequence |
||
241 | * @return array|string The generated random sequence |
||
242 | * @throws \InvalidArgumentException If the choices or length is invalid |
||
243 | */ |
||
244 | 18 | public function getSequence($choices, $length) |
|
258 | |||
259 | /** |
||
260 | * Returns the selected list of values for the sequence. |
||
261 | * @param array $values List of possible values |
||
262 | * @param int $length Number of values to return |
||
263 | * @return array Selected list of values for the sequence |
||
264 | * @throws \InvalidArgumentException If the value set is empty |
||
265 | */ |
||
266 | 15 | private function getSequenceValues(array $values, $length) |
|
285 | |||
286 | /** |
||
287 | * Returns a random UUID version 4 identifier. |
||
288 | * @return string A random UUID identifier |
||
289 | */ |
||
290 | public function getUuid() |
||
301 | } |
||
302 |