Skip to content

Instantly share code, notes, and snippets.

@webinista
Created December 18, 2014 19:30
Show Gist options
  • Save webinista/2f55d653f1bbf60e4677 to your computer and use it in GitHub Desktop.
Save webinista/2f55d653f1bbf60e4677 to your computer and use it in GitHub Desktop.
Command-line PHP script for generating random-ish passwords.
<?php
/*
* Designed to be used on the command line.
* php genpass.php [arg1] [arg2] ...
* Accepts up to three parameters:
* --length or -l = length of the password
* --specialchars or -s = whether to include punctuation in generating the password
* --unique or -u = whether to prevent repeated characters in a string
*
*/
$short = 'l::s::u::';
$long = array('length::','special::','unique::');
$options = getopt($short, $long);
# Checks whether both --length and -l are set. If they're different, throw an error.
if( isset( $options['length']) && isset( $options['l']) ):
if( $options['length'] !== $options['l'] ):
throw new Exception('Both -l and --length were set and they have different values. Please use just one.');
endif;
endif;
# Checks whether both --special and -u are set. If they're different, throw an error.
if( isset( $options['special']) && isset( $options['s']) ):
if( $options['s'] !== $options['special'] ):
throw new Exception('Both -s and --special were set and they have different values. Please use just one.');
endif;
endif;
# Checks whether both --unique and -u are set. If they're different, throw an error.
if( isset( $options['unique']) && isset( $options['u']) ):
if( $options['unique'] !== $options['u'] ):
throw new Exception('Both -u and --unique were set and they have different values. Please use just one.');
endif;
endif;
if( isset( $options['length']) ):
$length = (int)$options['length'];
elseif ( isset( $options['l']) ):
$length = (int)$options['l'];
endif;
if( isset( $options['special']) ):
$specialchars = $options['special'];
elseif ( isset( $options['s']) ):
$specialchars = $options['s'];
else:
$specialchars = 'true';
endif;
if( isset( $options['unique']) ):
$unique = $options['unique'];
elseif ( isset( $options['u']) ):
$unique = $options['u'];
else:
$unique = 'false';
endif;
function genpass($l=8, $sc='true', $u='false') {
$password = '';
$x = 0;
$lower_alpha = range('a','z');
$upper_alpha = range('A','Z');
$digits = range(0,9);
$punc = explode(',','!,@,#,$,%,^,&,*,(,),+,{,},[,],=,-,_');
if( $sc == 'true' ){
$allowed = array_merge($lower_alpha, $upper_alpha, $digits, $punc);
} else {
$allowed = array_merge($lower_alpha, $upper_alpha, $digits);
}
$allowed_length = count($allowed);
while($x < $l){
$pick = rand(0,$allowed_length-1);
# This is a bug. It's not returning uniques.
if($u ==='true'):
# pick a unique character. array_slice returns an array. we need the first member.
$password .= array_slice($allowed, $pick, 1, false)[0];
else:
$password .= $allowed[$pick];
endif;
$x++;
}
return $password;
}
try{
$password = genpass($length, $specialchars, $unique);
print PHP_EOL.PHP_EOL.PHP_EOL;
print $password;
print PHP_EOL.PHP_EOL.PHP_EOL;
} catch(Exception $e) {
print $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment