From 8447bae77c19a2350bd48b0f0c5b3a56a35c7af9 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak <alec@alec.pl> Date: Sun, 28 Jun 2015 06:27:48 -0400 Subject: [PATCH] Require Mbstring and OpenSSL extensions (#1490415) - remove redundant code --- program/lib/Roundcube/rcube_utils.php | 27 ++++++++++++--------------- 1 files changed, 12 insertions(+), 15 deletions(-) diff --git a/program/lib/Roundcube/rcube_utils.php b/program/lib/Roundcube/rcube_utils.php index 23fc3f8..db6c24c 100644 --- a/program/lib/Roundcube/rcube_utils.php +++ b/program/lib/Roundcube/rcube_utils.php @@ -1123,28 +1123,25 @@ /** * Generate a ramdom string * - * @param int String length + * @param int $length String length + * @param bool $raw Return RAW data instead of hex * * @return string The generated random string */ - public static function random_bytes($length) + public static function random_bytes($length, $raw = false) { - if (function_exists('openssl_random_pseudo_bytes')) { - $random = openssl_random_pseudo_bytes(ceil($length / 2)); - $random = bin2hex($random); + $rlen = $raw ? $length : ceil($length / 2); + $random = openssl_random_pseudo_bytes($rlen); - // if the length wasn't even... - if ($length < strlen($random)) { - $random = substr($random, 0, $length); - } + if ($raw) { + return $random; } - else { - $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_='; - $random = ''; - for ($i = 0; $i < $length; $i++) { - $random .= $alpha[rand(0, strlen($alpha)-1)]; - } + $random = bin2hex($random); + + // if the length wasn't even... + if ($length < strlen($random)) { + $random = substr($random, 0, $length); } return $random; -- Gitblit v1.9.1