Skip to content

Instantly share code, notes, and snippets.

@h3xx
Last active August 29, 2015 14:19
Show Gist options
  • Save h3xx/067a074c56f2b8dc1459 to your computer and use it in GitHub Desktop.
Save h3xx/067a074c56f2b8dc1459 to your computer and use it in GitHub Desktop.
Bad Code for generating a "random" password
/**
* 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