mt_rand is not random enough

From reporter:

protected function generate() { 
$characters = '123456789abcdefghijklmnpqrstuvwxyz'; 
$numChars = strlen($characters); 
$string = ''; 
for ($p = 0; $p < $this->codeLength; $p++) { 
do { 
$n = ord(drupal_random_bytes(1)); 
} while ($n >= 238); // 238 = 256 - 256 % $numChars 
$string .= $characters[$n % $numChars]; 
} 
return $string; 
}

Comments

coltrane’s picture

Status: Active » Needs review
StatusFileSize
new855 bytes

  • coltrane committed 4100355 on 7.x-2.x
    Issue #2332445 by coltrane: Fixed Use better random source.
    
coltrane’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jerebe’s picture

Priority: Normal » Major
Status: Closed (fixed) » Needs review

Hi,

I've just tried your latest patch and i encounter some issue implementing it.

On this line :
while(strlen($string <= $this->codeLength));
however
strlen($string <= $this->codeLength) return 1 since you are comparing a string and an integer and generates infinite loop.

A quick fix could be : while(strlen($string) <= $this->codeLength);

Regards

DanielFbrg’s picture

+1 confirmed

coltrane’s picture

StatusFileSize
new583 bytes

Thanks @jerebe, good catch.

  • coltrane committed 4f652b8 on 7.x-2.x
    Issue #2332445: Fix for infinite loop on generate
    
coltrane’s picture

Status: Needs review » Fixed

Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kristofferwiklund’s picture

Could we have a new release of the module, because of this. It breaks login completely.