chg: [internal] Remove random_compat

pull/7986/head
Jakub Onderka 2021-11-24 14:07:06 +01:00
parent b100377a73
commit b34d9bf2f8
3 changed files with 7 additions and 34 deletions

4
.gitmodules vendored
View File

@ -11,10 +11,6 @@
[submodule "app/files/warninglists"]
path = app/files/warninglists
url = https://github.com/MISP/misp-warninglists.git
[submodule "app/Lib/random_compat"]
path = app/Lib/random_compat
url = https://github.com/paragonie/random_compat
branch = master
[submodule "app/files/misp-galaxy"]
path = app/files/misp-galaxy
url = https://github.com/MISP/misp-galaxy

View File

@ -2,16 +2,6 @@
class RandomTool
{
public function __construct()
{
// import compatibility library for PHP < 7.0
if (!function_exists('random_int')) {
if (file_exists(APP . 'Lib' . DS . 'random_compat' . DS . 'lib' . DS . 'random.php')) {
require_once(APP . 'Lib' . DS . 'random_compat' . DS . 'lib' . DS . 'random.php');
}
}
}
/**
* Generate a random string
*
@ -27,24 +17,19 @@ class RandomTool
* @param int $length - How long should our random string be?
* @param string $charset - A string of all possible characters to choose from
* @return string
* @throws Exception
*/
public function random_str($crypto_secure = true, $length = 32, $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
// Type checks:
if (!is_bool($crypto_secure)) {
throw new InvalidArgumentException(
'random_str - Argument 1 - expected a boolean'
);
throw new InvalidArgumentException('random_str - Argument 1 - expected a boolean');
}
if (!is_numeric($length)) {
throw new InvalidArgumentException(
'random_str - Argument 2 - expected an integer'
);
throw new InvalidArgumentException('random_str - Argument 2 - expected an integer');
}
if (!is_string($charset)) {
throw new InvalidArgumentException(
'random_str - Argument 3 - expected a string'
);
throw new InvalidArgumentException('random_str - Argument 3 - expected a string');
}
if ($length < 1) {
@ -53,25 +38,18 @@ class RandomTool
}
// Remove duplicate characters from $charset
$split = str_split($charset);
$charset = implode('', array_unique($split));
$charset = count_chars($charset, 3);
// This is the maximum index for all of the characters in the string $charset
$charset_max = strlen($charset) - 1;
if ($charset_max < 1) {
// Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...'
throw new LogicException(
'random_str - Argument 3 - expected a string that contains at least 2 distinct characters'
);
throw new LogicException('random_str - Argument 3 - expected a string that contains at least 2 distinct characters');
}
// Now that we have good data, this is the meat of our function:
$random_str = '';
for ($i = 0; $i < $length; ++$i) {
if ($crypto_secure && function_exists('random_int')) {
$r = random_int(0, $charset_max);
} else {
$r = mt_rand(0, $charset_max);
}
$r = $crypto_secure ? random_int(0, $charset_max) : mt_rand(0, $charset_max);
$random_str .= $charset[$r];
}
return $random_str;

@ -1 +0,0 @@
Subproject commit 088c04e2f261c33bed6ca5245491cfca69195ccf