Skip to content

Instantly share code, notes, and snippets.

@nahanil
Last active November 29, 2016 01:57
Show Gist options
  • Save nahanil/856afc6b469bbe6d88fbf57dcb88f158 to your computer and use it in GitHub Desktop.
Save nahanil/856afc6b469bbe6d88fbf57dcb88f158 to your computer and use it in GitHub Desktop.
Generating "Very Strong" Plesk passwords
<?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);
}
<?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