note 16528 deleted from function.crypt by jimw

From: Date: Sat, 03 Nov 2001 18:21:16 +0000
Subject: note 16528 deleted from function.crypt by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-20379@lists.php.net to get a copy of this message
For those of you using the above examples: <p> The rannum() function provided by lasse@email.dk is fundamentally flawed. It calls mt_srand() to initialize the random number generator every time it generates a new random number with mt_rand(). This is a BAD idea, making the random number stream potentially very easy to duplicate. For security applications this is a classic failure which should be avoided. <p> The genchr() function in the same posting is also poorly written. The technique of using a while() loop to iterate over random number generation until numbers with the desired properties are found has two deficiencies: (1) it's unnecessarily inefficient, and (2) it skews the distribution of the numbers generated, making them potentially easier to predict or, as is more likely, reducing the search space significantly opening up the possibility of an efficient brute-force attack (even long after the fact if the approximate time of salt generation can be determined, which is often the case). <p> Taken together these two problems make for a piss-poor salt generation routine. Seeing rtdean@cytherianage.net's extension of the faulty salt functions to generate longer salt strings, the following is an attempt at a replacement for function "saltstr()" which generates a salt-usable random character string of a specified length, without falling prey to the problems I discuss above: <p> function salt_string($length){ for($i=0;$i<$length;$i++) { $num = mt_rand(1,64); $char = ( chr( ($num < 13 ? ($num + 34) : ($num < 39 ? ($num + 45) : ($num + 58) ) ) ) ); $string = $string . $char; } return $string; } <p> Note that you should call mt_srand() *only once* somewhere external to this function.

« previous php.notes (#20379) next »