mirror of https://github.com/MISP/MISP
chg: [internal] Remove random_compat
parent
b100377a73
commit
b34d9bf2f8
|
@ -11,10 +11,6 @@
|
||||||
[submodule "app/files/warninglists"]
|
[submodule "app/files/warninglists"]
|
||||||
path = app/files/warninglists
|
path = app/files/warninglists
|
||||||
url = https://github.com/MISP/misp-warninglists.git
|
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"]
|
[submodule "app/files/misp-galaxy"]
|
||||||
path = app/files/misp-galaxy
|
path = app/files/misp-galaxy
|
||||||
url = https://github.com/MISP/misp-galaxy
|
url = https://github.com/MISP/misp-galaxy
|
||||||
|
|
|
@ -2,16 +2,6 @@
|
||||||
|
|
||||||
class RandomTool
|
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
|
* Generate a random string
|
||||||
*
|
*
|
||||||
|
@ -27,24 +17,19 @@ class RandomTool
|
||||||
* @param int $length - How long should our random string be?
|
* @param int $length - How long should our random string be?
|
||||||
* @param string $charset - A string of all possible characters to choose from
|
* @param string $charset - A string of all possible characters to choose from
|
||||||
* @return string
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function random_str($crypto_secure = true, $length = 32, $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
public function random_str($crypto_secure = true, $length = 32, $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
||||||
{
|
{
|
||||||
// Type checks:
|
// Type checks:
|
||||||
if (!is_bool($crypto_secure)) {
|
if (!is_bool($crypto_secure)) {
|
||||||
throw new InvalidArgumentException(
|
throw new InvalidArgumentException('random_str - Argument 1 - expected a boolean');
|
||||||
'random_str - Argument 1 - expected a boolean'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (!is_numeric($length)) {
|
if (!is_numeric($length)) {
|
||||||
throw new InvalidArgumentException(
|
throw new InvalidArgumentException('random_str - Argument 2 - expected an integer');
|
||||||
'random_str - Argument 2 - expected an integer'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (!is_string($charset)) {
|
if (!is_string($charset)) {
|
||||||
throw new InvalidArgumentException(
|
throw new InvalidArgumentException('random_str - Argument 3 - expected a string');
|
||||||
'random_str - Argument 3 - expected a string'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($length < 1) {
|
if ($length < 1) {
|
||||||
|
@ -53,25 +38,18 @@ class RandomTool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove duplicate characters from $charset
|
// Remove duplicate characters from $charset
|
||||||
$split = str_split($charset);
|
$charset = count_chars($charset, 3);
|
||||||
$charset = implode('', array_unique($split));
|
|
||||||
|
|
||||||
// This is the maximum index for all of the characters in the string $charset
|
// This is the maximum index for all of the characters in the string $charset
|
||||||
$charset_max = strlen($charset) - 1;
|
$charset_max = strlen($charset) - 1;
|
||||||
if ($charset_max < 1) {
|
if ($charset_max < 1) {
|
||||||
// Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...'
|
// Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...'
|
||||||
throw new LogicException(
|
throw new LogicException('random_str - Argument 3 - expected a string that contains at least 2 distinct characters');
|
||||||
'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:
|
// Now that we have good data, this is the meat of our function:
|
||||||
$random_str = '';
|
$random_str = '';
|
||||||
for ($i = 0; $i < $length; ++$i) {
|
for ($i = 0; $i < $length; ++$i) {
|
||||||
if ($crypto_secure && function_exists('random_int')) {
|
$r = $crypto_secure ? random_int(0, $charset_max) : mt_rand(0, $charset_max);
|
||||||
$r = random_int(0, $charset_max);
|
|
||||||
} else {
|
|
||||||
$r = mt_rand(0, $charset_max);
|
|
||||||
}
|
|
||||||
$random_str .= $charset[$r];
|
$random_str .= $charset[$r];
|
||||||
}
|
}
|
||||||
return $random_str;
|
return $random_str;
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit 088c04e2f261c33bed6ca5245491cfca69195ccf
|
|
Loading…
Reference in New Issue