Last active
November 29, 2016 01:57
-
-
Save nahanil/856afc6b469bbe6d88fbf57dcb88f158 to your computer and use it in GitHub Desktop.
Generating "Very Strong" Plesk passwords
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
<?php | |
function generatePassword($stronger = true) { | |
$password = ""; | |
$length = $stronger ? 16 : 8; | |
$sets = array( | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
'abcdefghijklmnopqrstuvwxyz', | |
'0123456789', | |
'!@#$%^&*?_~', | |
); | |
// The minimum requirements of a "Very Strong" password | |
$types = [0, 0, 1, 1, 2, 2, 3]; | |
foreach ($types AS $type) { | |
$randIdx = floor(strlen($sets[$type]) * lcg_value()); | |
$password .= substr($sets[$type], $randIdx, 1); | |
} | |
// Padd out the length of password string | |
for ($i = strlen($password); $i < $length; $i++) { | |
$typeIdx = floor(count($sets) * lcg_value()); | |
$charIdx = floor(strlen($sets[$typeIdx]) * lcg_value()); | |
$password .= substr($sets[$typeIdx], $charIdx, 1); | |
} | |
// Shuffle things up a bit | |
return str_shuffle($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
<?php | |
$n = 10000; | |
echo "Checking $n passwords meet strength requirements\n"; | |
for ($i = 0; $i < $n; $i++) { | |
$pw = generatePassword(); | |
if ( | |
strlen($pw) < 16 | |
|| preg_match_all("/[A-Z]/", $pw) < 2 | |
|| preg_match_all("/[a-z]/", $pw) < 2 | |
|| preg_match_all("/[0-9]/", $pw) < 2 | |
|| preg_match_all("/[!@#$%^&*?_~]/", $pw) < 1 | |
) { | |
echo "FAILED! - ", $pw, "\n"; | |
} | |
} | |
echo "\n ! Done"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment