Created
June 15, 2016 13:55
-
-
Save softon/3a2375dd119d72f1e17e06ee25fd4245 to your computer and use it in GitHub Desktop.
Strong Password Generator
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
<?php | |
/** | |
* Code Snippet for Strong Password Generation | |
* | |
* @param int $length | |
* @param bool|false $add_dashes | |
* @param string $available_sets | |
* @return string | |
*/ | |
function generateStrongPassword($length = 9, $add_dashes = false, $available_sets = 'luds') | |
{ | |
$sets = array(); | |
if(strpos($available_sets, 'l') !== false) | |
$sets[] = 'abcdefghjkmnpqrstuvwxyz'; | |
if(strpos($available_sets, 'u') !== false) | |
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ'; | |
if(strpos($available_sets, 'd') !== false) | |
$sets[] = '23456789'; | |
if(strpos($available_sets, 's') !== false) | |
$sets[] = '!@#$%&*?'; | |
$all = ''; | |
$password = ''; | |
foreach($sets as $set) | |
{ | |
$password .= $set[array_rand(str_split($set))]; | |
$all .= $set; | |
} | |
$all = str_split($all); | |
for($i = 0; $i < $length - count($sets); $i++) | |
$password .= $all[array_rand($all)]; | |
$password = str_shuffle($password); | |
if(!$add_dashes) | |
return $password; | |
$dash_len = floor(sqrt($length)); | |
$dash_str = ''; | |
while(strlen($password) > $dash_len) | |
{ | |
$dash_str .= substr($password, 0, $dash_len) . '-'; | |
$password = substr($password, $dash_len); | |
} | |
$dash_str .= $password; | |
return $dash_str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment