Last active
August 29, 2015 14:19
-
-
Save h3xx/067a074c56f2b8dc1459 to your computer and use it in GitHub Desktop.
Bad Code for generating a "random" password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generate a random alphanumeric password | |
* precondition: len must be <= 32 | |
* param len: length of the random password | |
* param enforce_rules: enforce password security rules | |
* returns: the random password | |
**/ | |
public static function generateRandomPassword($len=8, $enforce_rules = false) | |
{ | |
$count = 0; | |
$valid = true; | |
do { | |
$password = substr(md5(microtime()), 0, $len); | |
if ($enforce_rules) | |
{ | |
try { | |
AuthUtility::checkPassword($password); | |
$valid = true; | |
} | |
catch (Exception $e) { | |
$valid = false; | |
} | |
} | |
} while ($valid === false && ++$count < 10); | |
if ($count >= 10) | |
{ | |
Log::writeln("Unable to generate random password!"); | |
$password = "6f33b5a3"; | |
} | |
return $password; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment