From b34d9bf2f87548d8e3bd06f569f710aad96d5750 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Wed, 24 Nov 2021 14:07:06 +0100 Subject: [PATCH] chg: [internal] Remove random_compat --- .gitmodules | 4 ---- app/Lib/Tools/RandomTool.php | 36 +++++++----------------------------- app/Lib/random_compat | 1 - 3 files changed, 7 insertions(+), 34 deletions(-) delete mode 160000 app/Lib/random_compat diff --git a/.gitmodules b/.gitmodules index c54dea32b..c7a9da43c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/app/Lib/Tools/RandomTool.php b/app/Lib/Tools/RandomTool.php index 4d50b8a50..52439c129 100644 --- a/app/Lib/Tools/RandomTool.php +++ b/app/Lib/Tools/RandomTool.php @@ -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; diff --git a/app/Lib/random_compat b/app/Lib/random_compat deleted file mode 160000 index 088c04e2f..000000000 --- a/app/Lib/random_compat +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 088c04e2f261c33bed6ca5245491cfca69195ccf