Last active
August 29, 2015 14:04
-
-
Save amlang/addae02bc39344d01f76 to your computer and use it in GitHub Desktop.
PHP - Method generates a random password
This file contains hidden or 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
/** | |
* Method generates a password | |
* @param int $length length of password | |
* @param int $strength | |
* 0 - Password with random case-sensitive characters and digits (standard set of 64 characters | |
* 1 - Password will be generated of a extended set of 88 characters | |
* @return string the generated password | |
*/ | |
public function generatePassword($length=8, $strength=1) { | |
$password = ''; | |
//standard set of 64 characters | |
$chars = '!#%+23456789:=?@ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | |
if ($stength) { | |
// extended set of 88 characters | |
$chars = '!"#$%&()*+,-./23456789:;<=>?@ABCDEFGHJKLMNOPRSTUVWXYZ[\]^_abcdefghijkmnopqrstuvwxyz{|}~'."'"; | |
//password length at least 12 characters | |
$length = ($length <= 12) ? 12 : $length; | |
} | |
$max = strlen($chars) - 1; | |
for ($i = 0; $i < $length; $i++) { | |
$password .= $chars[mt_rand(0, $max)]; | |
} | |
return $password; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment