Skip to content

Commit 835b51f

Browse files
committed
wp_rand() - more randy rands
git-svn-id: https://develop.svn.wordpress.org/trunk@8728 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 15bee9c commit 835b51f

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

wp-includes/pluggable.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,11 +1351,53 @@ function wp_generate_password($length = 12, $special_chars = true) {
13511351

13521352
$password = '';
13531353
for ( $i = 0; $i < $length; $i++ )
1354-
$password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
1354+
$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
13551355
return $password;
13561356
}
13571357
endif;
13581358

1359+
if ( !function_exists('wp_rand') ) :
1360+
/**
1361+
* Generates a random number
1362+
*
1363+
* @since 2.6.2
1364+
*
1365+
* @param int $min Lower limit for the generated number (optional, default is 0)
1366+
* @param int $max Upper limit for the generated number (optional, default is 4294967295)
1367+
* @return int A random number between min and max
1368+
*/
1369+
function wp_rand( $min = 0, $max = 0 ) {
1370+
global $rnd_value;
1371+
1372+
$seed = get_option('random_seed');
1373+
1374+
// Reset $rnd_value after 14 uses
1375+
// 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
1376+
if ( strlen($rnd_value) < 8 ) {
1377+
$rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
1378+
$rnd_value .= sha1($rnd_value);
1379+
$rnd_value .= sha1($rnd_value . $seed);
1380+
$seed = md5($seed . $rnd_value);
1381+
update_option('random_seed', $seed);
1382+
}
1383+
1384+
// Take the first 8 digits for our value
1385+
$value = substr($rnd_value, 0, 8);
1386+
1387+
// Strip the first eight, leaving the remainder for the next call to wp_rand().
1388+
$rnd_value = substr($rnd_value, 8);
1389+
1390+
$value = abs(hexdec($value));
1391+
1392+
// Reduce the value to be within the min - max range
1393+
// 4294967295 = 0xffffffff = max random number
1394+
if ( $max != 0 )
1395+
$value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
1396+
1397+
return abs(intval($value));
1398+
}
1399+
endif;
1400+
13591401
if ( !function_exists('wp_set_password') ) :
13601402
/**
13611403
* Updates the user's password with a new encrypted one.

0 commit comments

Comments
 (0)