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

This commit is contained in:
Ilya Tregubov
2023-08-17 09:13:18 +02:00
committed by Sara Arjona
5 changed files with 27 additions and 91 deletions
+2 -44
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