diff options
Diffstat (limited to 'vendor/paragonie/random_compat/lib/random_bytes_mcrypt.php')
-rw-r--r-- | vendor/paragonie/random_compat/lib/random_bytes_mcrypt.php | 87 |
1 files changed, 44 insertions, 43 deletions
diff --git a/vendor/paragonie/random_compat/lib/random_bytes_mcrypt.php b/vendor/paragonie/random_compat/lib/random_bytes_mcrypt.php index 7ac9d910..aac9c013 100644 --- a/vendor/paragonie/random_compat/lib/random_bytes_mcrypt.php +++ b/vendor/paragonie/random_compat/lib/random_bytes_mcrypt.php @@ -4,8 +4,8 @@ * for using the new PHP 7 random_* API in PHP 5 projects * * The MIT License (MIT) - * - * Copyright (c) 2015 Paragon Initiative Enterprises + * + * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,51 +26,52 @@ * SOFTWARE. */ +if (!is_callable('random_bytes')) { + /** + * Powered by ext/mcrypt (and thankfully NOT libmcrypt) + * + * @ref https://bugs.php.net/bug.php?id=55169 + * @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386 + * + * @param int $bytes + * + * @throws Exception + * + * @return string + */ + function random_bytes($bytes) + { + try { + $bytes = RandomCompat_intval($bytes); + } catch (TypeError $ex) { + throw new TypeError( + 'random_bytes(): $bytes must be an integer' + ); + } -/** - * Powered by ext/mcrypt (and thankfully NOT libmcrypt) - * - * @ref https://bugs.php.net/bug.php?id=55169 - * @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386 - * - * @param int $bytes - * - * @throws Exception - * - * @return string - */ -function random_bytes($bytes) -{ - try { - $bytes = RandomCompat_intval($bytes); - } catch (TypeError $ex) { - throw new TypeError( - 'random_bytes(): $bytes must be an integer' - ); - } + if ($bytes < 1) { + throw new Error( + 'Length must be greater than 0' + ); + } - if ($bytes < 1) { - throw new Error( - 'Length must be greater than 0' - ); - } + $buf = @mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); + if ( + $buf !== false + && + RandomCompat_strlen($buf) === $bytes + ) { + /** + * Return our random entropy buffer here: + */ + return $buf; + } - $buf = @mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); - if ( - $buf !== false - && - RandomCompat_strlen($buf) === $bytes - ) { /** - * Return our random entropy buffer here: + * If we reach here, PHP has failed us. */ - return $buf; + throw new Exception( + 'Could not gather sufficient random data' + ); } - - /** - * If we reach here, PHP has failed us. - */ - throw new Exception( - 'Could not gather sufficient random data' - ); } |