Merge branch 'master_MDL-78698' of https://github.com/mattporritt/moodle

This commit is contained in:
Ilya Tregubov 2023-08-17 08:51:27 +08:00 committed by Sara Arjona
commit d7461c9eff
No known key found for this signature in database
5 changed files with 27 additions and 91 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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

View File

@ -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();
}
/**

View File

@ -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 ===