diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2012-09-29 16:44:02 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@statuscode.ch> | 2012-09-29 16:44:02 +0200 |
commit | ef57e9294b52b838f65c9896c6d85c2f8663c90b (patch) | |
tree | f7bc3d7724f4a9c81b01ecb2e8361991e7993b6f /lib/util.php | |
parent | 992c2c9d4bc20db1dc8170dcc60dc4438dc55eb0 (diff) | |
download | nextcloud-server-ef57e9294b52b838f65c9896c6d85c2f8663c90b.tar.gz nextcloud-server-ef57e9294b52b838f65c9896c6d85c2f8663c90b.zip |
Fallback for systems without openssl
Diffstat (limited to 'lib/util.php')
-rwxr-xr-x | lib/util.php | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/lib/util.php b/lib/util.php index 29ab2c34e96..310ca6afec9 100755 --- a/lib/util.php +++ b/lib/util.php @@ -437,9 +437,7 @@ class OC_Util { */ public static function callRegister() { // generate a random token. - $bytes = openssl_random_pseudo_bytes(10, $cstrong); - $hex = bin2hex($bytes); - $token = $hex; + $token = self::generate_random_bytes(20); // store the token together with a timestamp in the session. $_SESSION['requesttoken-'.$token]=time(); @@ -550,4 +548,30 @@ class OC_Util { } } -} + /* + * @brief Generates random bytes with "openssl_random_pseudo_bytes" with a fallback for systems without openssl + * Inspired by gorgo on php.net + * @param Int with the length of the random + * @return String with the random bytes + */ + public static function generate_random_bytes($length = 30) { + if(function_exists('openssl_random_pseudo_bytes')) { + $pseudo_byte = bin2hex(openssl_random_pseudo_bytes($length, $strong)); + if($strong == TRUE) { + return substr($pseudo_byte, 0, $length); // Truncate it to match the length + } + } + + // fallback to mt_rand() + $characters = '0123456789'; + $characters .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + $charactersLength = strlen($characters)-1; + $pseudo_byte = ""; + + // Select some random characters + for ($i = 0; $i < $length; $i++) { + $pseudo_byte .= $characters[mt_rand(0, $charactersLength)]; + } + return $pseudo_byte; + } +}
\ No newline at end of file |