aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB
diff options
context:
space:
mode:
authorMarc Alexander <admin@m-a-styles.de>2018-09-17 21:01:45 +0200
committerMarc Alexander <admin@m-a-styles.de>2018-09-17 21:01:45 +0200
commit9000936926849bdf82db232361e578ced1748512 (patch)
treeca25d44f47f25000cf064a6e5567d7f9c13e01cb /phpBB
parent9503996441f7975447b9531317a0b0af685f127b (diff)
parentced8599e306fdbc5e496b3119ef46c4c27e3e335 (diff)
downloadforums-9000936926849bdf82db232361e578ced1748512.tar
forums-9000936926849bdf82db232361e578ced1748512.tar.gz
forums-9000936926849bdf82db232361e578ced1748512.tar.bz2
forums-9000936926849bdf82db232361e578ced1748512.tar.xz
forums-9000936926849bdf82db232361e578ced1748512.zip
Merge pull request #5292 from rubencm/ticket/15723
[ticket/15723] Rewrite gen_rand_string() and gen_rand_string_friendly()
Diffstat (limited to 'phpBB')
-rw-r--r--phpBB/includes/functions.php29
1 files changed, 21 insertions, 8 deletions
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php
index cbea7afe6e..1457888c9f 100644
--- a/phpBB/includes/functions.php
+++ b/phpBB/includes/functions.php
@@ -73,8 +73,17 @@ function set_var(&$result, $var, $type, $multibyte = false)
*/
function gen_rand_string($num_chars = 8)
{
- // [a, z] + [0, 9] = 36
- return substr(strtoupper(base_convert(bin2hex(random_bytes($num_chars + 1)), 16, 36)), 0, $num_chars);
+ $range = array_merge(range('A', 'Z'), range(0, 9));
+ $size = count($range);
+
+ $output = '';
+ for ($i = 0; $i < $num_chars; $i++)
+ {
+ $rand = random_int(0, $size-1);
+ $output .= $range[$rand];
+ }
+
+ return $output;
}
/**
@@ -88,13 +97,17 @@ function gen_rand_string($num_chars = 8)
*/
function gen_rand_string_friendly($num_chars = 8)
{
- $rand_str = bin2hex(random_bytes($num_chars + 1));
+ $range = array_merge(range('A', 'N'), range('P', 'Z'), range(1, 9));
+ $size = count($range);
- // Remove Z and Y from the base_convert(), replace 0 with Z and O with Y
- // [a, z] + [0, 9] - {z, y} = [a, z] + [0, 9] - {0, o} = 34
- $rand_str = str_replace(array('0', 'O'), array('Z', 'Y'), strtoupper(base_convert($rand_str, 16, 34)));
+ $output = '';
+ for ($i = 0; $i < $num_chars; $i++)
+ {
+ $rand = random_int(0, $size-1);
+ $output .= $range[$rand];
+ }
- return substr($rand_str, 0, $num_chars);
+ return $output;
}
/**
@@ -102,7 +115,7 @@ function gen_rand_string_friendly($num_chars = 8)
*/
function unique_id()
{
- return bin2hex(random_bytes(8));
+ return gen_rand_string(32);
}
/**