Skip to content

Instantly share code, notes, and snippets.

@derak-kilgo
Created March 26, 2015 20:09
Show Gist options
  • Save derak-kilgo/869ac055164c87a37392 to your computer and use it in GitHub Desktop.
Save derak-kilgo/869ac055164c87a37392 to your computer and use it in GitHub Desktop.
php cli password generator
#!/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.
@derak-kilgo
Copy link
Author

Could be condensed into a single command of
php -r "echo base64_encode(openssl_random_pseudo_bytes(32)) . chr(10);"

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