diff --git a/admin/tool/generator/classes/course_backend.php b/admin/tool/generator/classes/course_backend.php index e24ecbdd29d..218d5cc07ec 100644 --- a/admin/tool/generator/classes/course_backend.php +++ b/admin/tool/generator/classes/course_backend.php @@ -454,7 +454,7 @@ class tool_generator_course_backend extends tool_generator_backend { // Generate random binary data (different for each file so it // doesn't compress unrealistically). - $data = random_bytes_emulate($this->limit_filesize(self::$paramsmallfilesize[$this->size])); + $data = random_bytes($this->limit_filesize(self::$paramsmallfilesize[$this->size])); $fs->create_file_from_string($filerecord, $data); $this->dot($i, $count); @@ -495,7 +495,7 @@ class tool_generator_course_backend extends tool_generator_backend { throw new coding_exception('Failed to open temporary file'); } for ($j = 0; $j < $blocks; $j++) { - $data = random_bytes_emulate($blocksize); + $data = random_bytes($blocksize); fwrite($handle, $data); $this->dot($i * $blocks + $j, $count * $blocks); } diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 573e1774039..e501f2455aa 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3483,3 +3483,24 @@ function theme_get_locked_theme_for_device($device) { $themeconfigname = core_useragent::get_device_type_cfg_var_name($device); return $CFG->config_php_settings[$themeconfigname]; } + +/** + * Try to generate cryptographically secure pseudo-random bytes. + * + * Note this is achieved by fallbacking between: + * - PHP 7 random_bytes(). + * - OpenSSL openssl_random_pseudo_bytes(). + * - In house random generator getting its entropy from various, hard to guess, pseudo-random sources. + * + * @param int $length requested length in bytes + * @deprecated since 4.3. + * @return string binary data + */ +function random_bytes_emulate($length) { + debugging( + __FUNCTION__ . '() is deprecated.' . + 'Please use random_bytes instead.', + DEBUG_DEVELOPER + ); + return random_bytes($length); +} diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 7a69fde8626..794bad1a051 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8456,7 +8456,7 @@ function count_letters($string, $format = null) { * @return string */ function random_string($length=15) { - $randombytes = random_bytes_emulate($length); + $randombytes = random_bytes($length); $pool = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $pool .= 'abcdefghijklmnopqrstuvwxyz'; $pool .= '0123456789'; @@ -8485,7 +8485,7 @@ function complex_random_string($length=null) { if ($length===null) { $length = floor(rand(24, 32)); } - $randombytes = random_bytes_emulate($length); + $randombytes = random_bytes($length); $string = ''; for ($i = 0; $i < $length; $i++) { $rand = ord($randombytes[$i]); @@ -8494,48 +8494,6 @@ function complex_random_string($length=null) { return $string; } -/** - * Try to generates cryptographically secure pseudo-random bytes. - * - * Note this is achieved by fallbacking between: - * - PHP 7 random_bytes(). - * - OpenSSL openssl_random_pseudo_bytes(). - * - In house random generator getting its entropy from various, hard to guess, pseudo-random sources. - * - * @param int $length requested length in bytes - * @return string binary data - */ -function random_bytes_emulate($length) { - global $CFG; - if ($length <= 0) { - debugging('Invalid random bytes length', DEBUG_DEVELOPER); - return ''; - } - if (function_exists('random_bytes')) { - // Use PHP 7 goodness. - $hash = @random_bytes($length); - if ($hash !== false) { - return $hash; - } - } - if (function_exists('openssl_random_pseudo_bytes')) { - // If you have the openssl extension enabled. - $hash = openssl_random_pseudo_bytes($length); - if ($hash !== false) { - return $hash; - } - } - - // Bad luck, there is no reliable random generator, let's just slowly hash some unique stuff that is hard to guess. - $staticdata = serialize($CFG) . serialize($_SERVER); - $hash = ''; - do { - $hash .= sha1($staticdata . microtime(true) . uniqid('', true), true); - } while (strlen($hash) < $length); - - return substr($hash, 0, $length); -} - /** * Given some text (which may contain HTML) and an ideal length, * this function truncates the text neatly on a word boundary if possible diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index d866fd4612e..3999d3f9a63 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -3873,35 +3873,6 @@ EOT; } - /* - * Test emulation of random_bytes() function. - */ - public function test_random_bytes_emulate() { - $result = random_bytes_emulate(10); - $this->assertSame(10, strlen($result)); - $this->assertnotSame($result, random_bytes_emulate(10)); - - $result = random_bytes_emulate(21); - $this->assertSame(21, strlen($result)); - $this->assertnotSame($result, random_bytes_emulate(21)); - - $result = random_bytes_emulate(666); - $this->assertSame(666, strlen($result)); - - $result = random_bytes_emulate(40); - $this->assertSame(40, strlen($result)); - - $this->assertDebuggingNotCalled(); - - $result = random_bytes_emulate(0); - $this->assertSame('', $result); - $this->assertDebuggingCalled(); - - $result = random_bytes_emulate(-1); - $this->assertSame('', $result); - $this->assertDebuggingCalled(); - } - /** * Test function for creation of random strings. */ @@ -3927,14 +3898,6 @@ EOT; $this->assertMatchesRegularExpression('/^[' . $pool . ']+$/', $result); $this->assertDebuggingNotCalled(); - - $result = random_string(0); - $this->assertSame('', $result); - $this->assertDebuggingCalled(); - - $result = random_string(-1); - $this->assertSame('', $result); - $this->assertDebuggingCalled(); } /** @@ -3962,14 +3925,6 @@ EOT; $this->assertMatchesRegularExpression('/^[' . $pool . ']+$/', $result); $this->assertDebuggingNotCalled(); - - $result = complex_random_string(0); - $this->assertSame('', $result); - $this->assertDebuggingCalled(); - - $result = complex_random_string(-1); - $this->assertSame('', $result); - $this->assertDebuggingCalled(); } /** diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 387c7450711..54e2529a57a 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -81,6 +81,8 @@ information provided here is intended especially for developers. - I choose "Group mode > Visible groups" in the open action menu * addHelpButton() function has a new optional $a parameter to allow variables with translation strings. * help_icon constructor has a new optional $a parameter to allow variables with translation strings. +* The random_bytes_emulate() function has been deprecated, because it's not required anymore. PHP native function random_bytes() + should be used instead. === 4.2 ===