Created
March 26, 2015 20:09
-
-
Save derak-kilgo/869ac055164c87a37392 to your computer and use it in GitHub Desktop.
php cli 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
#!/usr/bin/env php | |
<?php | |
#accept command line arg for length. | |
$options = getopt("l:"); | |
if(empty($options['l'])){ | |
$length = 32; | |
}else{ | |
$length = (int) $options['l']; | |
} | |
#may contain unprintable characters. Base64 encode for ease of use. Double the lenght to make sure we have enough data to clip. | |
$str = base64_encode(openssl_random_pseudo_bytes($length*2)); | |
#Only allow letters and numbers. | |
$str = preg_replace("/[^A-Za-z0-9]/","",$str); | |
#Clip to the requested lenght. Default is 32. | |
echo substr($str,0,$length) . chr(10); | |
#save as /usr/local/bin/makepassword, chmod a=x /usr/local/bin/makepassword | |
#Usage: makepassword -l8 for an 8 character password. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could be condensed into a single command of
php -r "echo base64_encode(openssl_random_pseudo_bytes(32)) . chr(10);"