• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/security/random.php
  • Класс: BitrixMainSecurityRandom
  • Вызов: Random::getBytes
static function getBytes($length)
{
	$backup = null;

	if ($length <= 0)
	{
		$length = 1;
	}

	$bytes = openssl_random_pseudo_bytes($length, $strong);
	if ($bytes && strlen($bytes) >= $length)
	{
		if ($strong)
		{
			return substr($bytes, 0, $length);
		}
		$backup = $bytes;
	}

	if (file_exists('/dev/urandom'))
	{
		if ($file = @fopen('/dev/urandom', 'rb'))
		{
			$bytes = @fread($file, $length + 1);
			@fclose($file);
			if ($bytes && strlen($bytes) >= $length)
			{
				return substr($bytes, 0, $length);
			}
		}
	}

	if ($backup && strlen($backup) >= $length)
	{
		return substr($backup, 0, $length);
	}

	$bytes = '';
	while (strlen($bytes) < $length)
	{
		$bytes .= static::getPseudoRandomBlock();
	}

	return substr($bytes, 0, $length);
}