gen_passwd.pl
#!/usr/local/bin/perl
# ----------------------------------------- */
# gen_passwd.pl
#
$version = "2.1.02";
# package: general security
#
# Author: pjohnson@bosconet.org
#
# Language: perl
# License: GNU general public license
# Input: a few simple arguements
# Output: a halfway decent password
# (c) 2008 by Paul M Johnson
#
# ----------------------------------------- */
# */
# Customization Instructions */
# */
# ----------------------------------------- */
#
# things you must do:
# things you might have to do:
#
# ----------------------------------------- */
# ---- modifications and fixes ------------ */
#
# 14 Aug 2000:
# -changed layout
# -modified srand statement
# -added mixed character support
# -added number character support
# -added special character support
# ::PMJ
# 23 Sept 2000
# -added command line help
# ::PMJ
# 09 Mar 2001
# -altered the -max behavior just
# truncate the genrate string
# ::PMJ
# 12 Mar 2003
# -altered to use GetOpt::Long for
# command line arguements
#
# ----------------------------------------- */
# ----------------------------------------- */
# CONFIGURABLE VARIABLES */
# ----------------------------------------- */
#
$max = 40; # maximum password len
$min = 8; # minimum password len
$uppercase = 0; # set uppercase OFF
$mixedcase = 1; # set mixed case ON
$special= 0; # set mixed case ON
$SEED=37;
$Passwords=1;
$help=0;
# ----------------------------------------- */
# end configurable variables section */
# ----------------------------------------- */
# ----------------------------------------- */
# require/use statements
# ----------------------------------------- */
use Getopt::Long;
# ----------------------------------------- */
# ----------------------------------------- */
# other variable assignments
# ----------------------------------------- */
$credits="\nBosconet Consulting password generator\nVersion $version\n(c)2000-2008 Paul M Johnson\nrelased under the GPL.\n";
$help_text="$credits\ngen_password [ --help] [--version ] [ --number passwords to genrate ]\n\t{ [ --lowercase use lowercase characters only ] |\n\t [ --uppercase use uppercase characters only ] |\n\t [--mixed use mixed lower and upper case letters (default)] |\n\t [ --special use special character (e.g. \$, &, !...) ] }\n\t [--max maximum length of password] |\n\t [--seed seed number for rand function]\n”;
@specialchars = ( “!”, “\(”, “\)”, “;”, “:”, “‘”, “\”", “?”, “,” );
# ———————i——————- */
# ———– MAIN PROCESSING ————- */
Getopt::Long::Configure qw(bundling no_ignore_case);
$result=GetOptions( ‘help!’ => \$help,
‘version!’ => ‘1′,
‘number=i’ => \$Passwords ,
‘lowercase!’ => \$lowercase,
‘uppercase!’ => \$uppercase,
‘mixed!’ => \$mixedcase,
’special!’ => \$special,
‘max=i’ => \$max,
’seed=i’ => \$SEED,
‘debug!’ => \$debug);
if ($help) {
print “$help_text\n”;
exit 0;
}
if ($version) {
print “$credits\n”;
}
if (! $SEDD ) {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$SEED = $sec;
}
srand(time ^ ($$ + ($$< <$SEED)));
while ($Passwords){
&GeneratePassword();
$Passwords--;
}
print "\n";
exit 0;
# ---------------------+
# |
# below here lie subs |
# |
# ---------------------+
sub SomeRandomChar {
$charnum = int(rand(26));
$charnum += 97;
$char = chr($charnum); # convert number to character.
if ($uppercase) { $char = uc($char); }
elsif ($special) {
if ((int(rand(2)))) {$char = uc($char);}
elsif ((int(rand(2)))) { $char = $char;}
elsif ((int(rand(2)))) {$char = $charnum = int(rand(9));}
elsif ((int(rand(2)))) {
$charnum = int(rand($#specialchars + 1));
$char = $specialchars[$charnum];
}
}
else {
if ((int(rand(2)))) {$char = uc($char);}
elsif ((int(rand(2)))) {$char = $charnum = int(rand(9));}
}
return $char;
} # end sub SomeRandomChar()
sub GeneratePassword {
$letters=0;
until ($letters > $min ) {
$letters = int(rand(40)) ;
}
$acro = $char = “”;
$i = 1; # initialize i
while ($i < $letters) {
&SomeRandomChar();
$acro .= $char; # append $char to $acro, which is "" to start with.
$i++;
}
$new_password = substr($acro,0,$max);
print "$new_password\n";
}