Fix regression in getRandomPassword

This commit is contained in:
Laurent Destailleur 2017-11-05 03:03:17 +01:00
parent a066bbcd89
commit 91950ccbc7
2 changed files with 17 additions and 14 deletions

View File

@ -454,18 +454,21 @@ function getRandomPassword($generic=false)
$uppercase = "ASDFGHJKLZXCVBNMQWERTYUIOP";
$numbers = "1234567890";
$randomCode = "";
$nbofchar = round($length/3);
$nbofcharlast = ($length - 2*$nbofchar);
var_dump($nbofchar.'-'.$nbofcharlast);
if (function_exists('random_int')) // Cryptographic random
{
$max = strlen($lowercase) - 1;
for ($x = 0; $x < abs($length/3); $x++) {
for ($x = 0; $x < $nbofchar; $x++) {
$randomCode .= $lowercase{random_int(0, $max)};
}
$max = strlen($uppercase) - 1;
for ($x = 0; $x < abs($length/3); $x++) {
for ($x = 0; $x < $nbofchar; $x++) {
$randomCode .= $uppercase{random_int(0, $max)};
}
$max = strlen($numbers) - 1;
for ($x = 0; $x < abs($length/3); $x++) {
for ($x = 0; $x < $nbofcharlast; $x++) {
$randomCode .= $numbers{random_int(0, $max)};
}
@ -474,15 +477,15 @@ function getRandomPassword($generic=false)
else // Old platform, non cryptographic random
{
$max = strlen($lowercase) - 1;
for ($x = 0; $x < abs($length/3); $x++) {
for ($x = 0; $x < $nbofchar; $x++) {
$randomCode .= $lowercase{mt_rand(0, $max)};
}
$max = strlen($uppercase) - 1;
for ($x = 0; $x < abs($length/3); $x++) {
for ($x = 0; $x < $nbofchar; $x++) {
$randomCode .= $uppercase{mt_rand(0, $max)};
}
$max = strlen($numbers) - 1;
for ($x = 0; $x < abs($length/3); $x++) {
for ($x = 0; $x < $nbofcharlast; $x++) {
$randomCode .= $numbers{mt_rand(0, $max)};
}

View File

@ -255,17 +255,17 @@ class SecurityTest extends PHPUnit_Framework_TestCase
$genpass1=getRandomPassword(true); // Should be a string return by dol_hash (if no option set, will be md5)
print __METHOD__." genpass1=".$genpass1."\n";
$this->assertEquals(strlen($genpass1),32);
$this->assertEquals(strlen($genpass1), 32);
$conf->global->USER_PASSWORD_GENERATED='None';
$genpass2=getRandomPassword(false); // Should be an empty string
print __METHOD__." genpass2=".$genpass2."\n";
$this->assertEquals($genpass2,'');
$this->assertEquals($genpass2, '');
$conf->global->USER_PASSWORD_GENERATED='Standard';
$genpass3=getRandomPassword(false);
print __METHOD__." genpass3=".$genpass3."\n";
$this->assertEquals(strlen($genpass3),8);
$this->assertEquals(strlen($genpass3), 8);
return 0;
}