Skip to content

Instantly share code, notes, and snippets.

@tylerhall
Created August 12, 2010 21:38
Star You must be signed in to star a gist
Save tylerhall/521810 to your computer and use it in GitHub Desktop.
A user friendly, strong password generator PHP function.
<?PHP
// Generates a strong password of N length containing at least one lower case letter,
// one uppercase letter, one digit, and one special character. The remaining characters
// in the password are chosen at random from those four sets.
//
// The available characters in each set are user friendly - there are no ambiguous
// characters such as i, l, 1, o, 0, etc. This, coupled with the $add_dashes option,
// makes it much easier for users to manually type or speak their passwords.
//
// Note: the $add_dashes option will increase the length of the password by
// floor(sqrt(N)) characters.
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;
}
Copy link

ghost commented Mar 31, 2015

Great Code

@Shifrin
Copy link

Shifrin commented May 3, 2015

@marklockhart
Copy link

Good work, thanks for the function

@kildeby
Copy link

kildeby commented Jun 30, 2015

I can't seem to make it display the password? How do I use it?

  • nevermind, I found out. Just sharing -> add:

@Shifrin: Your solution does not require a BIG LETTER, a small letter, a number (0-9) and a special character, which makes your password very much stronger.

@paragonie-scott
Copy link

Head's up: This gist is not secure. Neither array_rand() nor str_shuffle() are cryptographically secure.

Recommended reading: How to generate secure passwords in PHP.

@jasonglisson
Copy link

This is great! I needed the ability to force the use of certain characters to make the password. hanks! Good work!

Copy link

ghost commented Aug 12, 2016

thanks

@Xwalk-Company
Copy link

thks

@spurgeonbj
Copy link

God bless you!

@hsali
Copy link

hsali commented Oct 23, 2016

a good script.

@0ngsh
Copy link

0ngsh commented Nov 8, 2016

Thanks,Very Useful

@sarment0
Copy link

Good work!

@AlkarE
Copy link

AlkarE commented Jun 9, 2017

Thanks a lot!

@caionorder
Copy link

thanks!

@offixer
Copy link

offixer commented Aug 17, 2017

@marufnajm
Copy link

A very good script. Thank you so much.

Copy link

ghost commented Nov 6, 2017

thank you 👍

@NikitaShkaruba
Copy link

NikitaShkaruba commented Nov 20, 2017

Thank you dude! vk.com here)

@pishgahi
Copy link

thank you

@fabbro79
Copy link

thanks!

@compermisos
Copy link

make a forked version of this script, whit some improvments in crypt functions

https://gist.github.com/compermisos/cf11aed742d2e1fbd994e083b4b0fa78

Copy link

ghost commented May 11, 2018

See my implementation works with all chars languages.

@DonPramis
Copy link

DonPramis commented Aug 10, 2018

Here is easy https://www.dohangout.com/forum/viewtopic.php?f=23&t=333 no need to create password. 14 million password. Enjoy

@DonPramis
Copy link

DonPramis commented Aug 26, 2018

I generate mine like this... http://tools.webbatlas.com but i need to include few symbols on this project

@Cocoking77
Copy link

Thks a lot

@fmic64
Copy link

fmic64 commented Jun 10, 2020

Great! many many thanks!!!

@madurapa
Copy link

Amazing!

@jamesboss99
Copy link

Very good work. Thanks.

@Adhitya2525
Copy link

Thanks a lot

@agungsp2000
Copy link

thank you kind sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment