From: Hypolite Petovan Date: Sun, 26 Nov 2017 12:57:29 +0000 (-0500) Subject: Add odd number of character support to random_string() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d5c38b9fc9c51112d3870d7237121840eb0e2d99;p=friendica.git Add odd number of character support to random_string() --- diff --git a/include/text.php b/include/text.php index f49ccc8abc..5df1d65cee 100644 --- a/include/text.php +++ b/include/text.php @@ -41,26 +41,19 @@ function replace_macros($s, $r) { return $output; } -// PHP < 7 polyfill -if (!is_callable('intdiv')) { - function intdiv($a, $b) { - return ($a - $a % $b) / $b; - } -} - /** * @brief Generates a pseudo-random string of hexadecimal characters * - * Only supports pair numbers of output characters. - * * @param int $size * @return string */ function random_string($size = 64) { - $bytes = random_bytes(intdiv((int) $size, 2)); + $byte_size = ceil($size / 2); + + $bytes = random_bytes($byte_size); - $return = bin2hex($bytes); + $return = substr(bin2hex($bytes), 0, $size); return $return; }