Skip to content

Instantly share code, notes, and snippets.

@weishuaiwang
Created December 3, 2012 02:37
Show Gist options
  • Save weishuaiwang/4192234 to your computer and use it in GitHub Desktop.
Save weishuaiwang/4192234 to your computer and use it in GitHub Desktop.
Generate random passwords that are highly secure
<?php
/**
* Generate random passwords that are highly secure
*
* @return string
* @param $level 1:easy 2:Normal 3:Hard
* @author
**/
function generatePassword($length = 6, $level = 2){
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$validchars[1] = '0123456789abcdfghjkmnpqrstvwxyz';
$validchars[2] = '0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$validchars[3] = '0123456789_!@#$%&*()-=+/abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%&*()-=+/';
$password = '';
$counter = 0;
while ($counter < $length) {
$actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
// All character must be different
if (!strstr($password, $actChar)) {
$password .= $actChar;
$counter++;
}
}
return $password;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment