From 9fc247408cdd171d37ebfc0de018fb740509557b Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Fri, 8 Jan 2016 03:27:58 +0000 Subject: [PATCH] Update to Random_Compat 1.1.5 to fix an issue with older libSodium modules. Props sarciszewski. Fixes #35327 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@36220 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/random_compat/random.php | 10 ++- .../random_bytes_libsodium_legacy.php | 84 +++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 src/wp-includes/random_compat/random_bytes_libsodium_legacy.php diff --git a/src/wp-includes/random_compat/random.php b/src/wp-includes/random_compat/random.php index 8898a35bcd..fb21ef2e01 100644 --- a/src/wp-includes/random_compat/random.php +++ b/src/wp-includes/random_compat/random.php @@ -58,7 +58,11 @@ if (PHP_VERSION_ID < 70000) { */ if (extension_loaded('libsodium')) { // See random_bytes_libsodium.php - require_once $RandomCompatDIR.'/random_bytes_libsodium.php'; + if (PHP_VERSION_ID >= 50300 && function_exists('\\Sodium\\randombytes_buf')) { + require_once $RandomCompatDIR.'/random_bytes_libsodium.php'; + } elseif (method_exists('Sodium', 'randombytes_buf')) { + require_once $RandomCompatDIR.'/random_bytes_libsodium_legacy.php'; + } } if ( !function_exists('random_bytes') && @@ -75,8 +79,8 @@ if (PHP_VERSION_ID < 70000) { // that is not helpful to us here. // See random_bytes_dev_urandom.php - require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php'; - } + require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php'; + } if ( !function_exists('random_bytes') && PHP_VERSION_ID >= 50307 && diff --git a/src/wp-includes/random_compat/random_bytes_libsodium_legacy.php b/src/wp-includes/random_compat/random_bytes_libsodium_legacy.php new file mode 100644 index 0000000000..4c76ff96d2 --- /dev/null +++ b/src/wp-includes/random_compat/random_bytes_libsodium_legacy.php @@ -0,0 +1,84 @@ + 2147483647) { + $buf = ''; + for ($i = 0; $i < $bytes; $i += 1073741824) { + $n = ($bytes - $i) > 1073741824 + ? 1073741824 + : $bytes - $i; + $buf .= Sodium::randombytes_buf($n); + } + } else { + $buf = Sodium::randombytes_buf($bytes); + } + + if ($buf !== false) { + if (RandomCompat_strlen($buf) === $bytes) { + return $buf; + } + } + + /** + * If we reach here, PHP has failed us. + */ + throw new Exception( + 'Could not gather sufficient random data' + ); +}