30 public static function getInt($min = 0, $max = \PHP_INT_MAX)
34 throw new \Bitrix\Main\ArgumentException(
35 'The min parameter must be lower than max parameter'
44 if ($range > \PHP_INT_MAX || is_float($range))
46 throw new \Bitrix\Main\SystemException(
47 'The supplied range is too great'
51 $bits = static::countBits($range) + 1;
52 $length = (int) max(ceil($bits / 8), 1);
53 $filter = pow(2, $bits) - 1;
54 if ($filter >= \PHP_INT_MAX)
55 $filter = \PHP_INT_MAX;
57 $filter = (int) $filter;
61 $rnd = hexdec(bin2hex(self::getBytes($length)));
62 $rnd = $rnd & $filter;
64 while ($rnd > $range);
117 $charsetVariants = strlen($charsetList);
118 $randomSequence = static::getBytes($length);
121 for ($i = 0; $i < $length; $i++)
123 $randomNumber = ord($randomSequence[$i]);
124 $result .= $charsetList[$randomNumber % $charsetVariants];
137 $count = count($charsetList);
141 foreach ($charsetList as $charset)
143 $charsets[] = [$charset, strlen($charset)];
146 $randomSequence = static::getBytes($length);
149 for ($i = 0; $i < $length; $i += $count)
153 for ($j = 0; $j < $count; $j++)
155 $randomNumber = ord($randomSequence[$i + $j]);
157 $charset = $charsets[$j][0];
158 $charsetVariants = $charsets[$j][1];
159 $result .= $charset[$randomNumber % $charsetVariants];
161 if (($i + $j + 1) == $length)
186 $bytes = openssl_random_pseudo_bytes($length, $strong);
187 if ($bytes && strlen($bytes) >= $length)
191 return substr($bytes, 0, $length);
196 if (file_exists(
'/dev/urandom'))
198 if ($file = @fopen(
'/dev/urandom',
'rb'))
200 $bytes = @fread($file, $length + 1);
202 if ($bytes && strlen($bytes) >= $length)
204 return substr($bytes, 0, $length);
209 if ($backup && strlen($backup) >= $length)
211 return substr($backup, 0, $length);
215 while (strlen($bytes) < $length)
217 $bytes .= static::getPseudoRandomBlock();
220 return substr($bytes, 0, $length);
230 $bytes = openssl_random_pseudo_bytes(static::RANDOM_BLOCK_LENGTH);
231 if ($bytes && strlen($bytes) >= static::RANDOM_BLOCK_LENGTH)
233 return substr($bytes, 0, static::RANDOM_BLOCK_LENGTH);
237 for ($i=0; $i < static::RANDOM_BLOCK_LENGTH; $i++)
239 $bytes .= pack(
'S', mt_rand(0,0xffff));
242 return hash(
'sha512', $bytes,
true);