#!/usr/local/bin/perl # Copyright (C) 1994-1997 - pckizer@tamu.edu # Change these to match best source of random data on your systems # WARNING: On many systems, a 'ps' is just too slow to be usable, # these should be an option so users can pick their own data. @random_seeds = ( "/dev/ksyms", "/etc/utmp", "/etc/utmpx", "/etc/wtmp", "netstat -n|"); use Getopt::Std; #require 'getopts.pl'; ($iam = $0) =~ s,.*/,,; &getopts('Hfrmnahpv:V') || &Usage; &Usage if $opt_h; sub Usage { $l=length($iam); print "Usage: $iam [-f] [-r] [-m] [-a] [-n] [-h] [-p] [pswds [chars]]\n"; print ' 'x($l+8),"Generate random passwords. The following options mean:\n"; print ' 'x($l+8),"-r means use a \"reasonable\" character set (default)\n"; print ' 'x($l+8),"-f means use the full printable character set\n"; print ' 'x($l+8),"-H means to use hexadecimal digits\n"; print ' 'x($l+8),"-m means use a minimal character set\n"; print ' 'x($l+8),"-a means use a just the alpha-numeric character set\n"; print ' 'x($l+8),"-n means use a just the numeric character set\n"; print ' 'x($l+8),"-p print character set to be used\n"; print ' 'x($l+8),"-v # is max, but allow # fewer characters\n"; print ' 'x($l+8),"-V allow 1 fewer characters (rand 7 or 8)\n"; print ' 'x($l+8),"pswds is the number of random passwords to generate\n"; print ' 'x($l+8),"chars is the max number of characters to generate\n"; exit(0); } if (($opt_f+$opt_m+$opt_a+$opt_n+$opt_r)>1) { print "$iam: error: please spacify only one of: -f -r -m -a -n\n"; exit(1); } # The "reasonable" character set @char=('a'..'z','A'..'Z','0'..'9',',','.','/','-','=','!','@','#','$','%','&'); if ($opt_f) { # Full @char=('a'..'z','A'..'Z','0'..'9','!','"','#','$','%','&','\'','(',')','*', '+',',','-','.','/',':',';','<','=','>','?','@','{','|','}','~'); } elsif ($opt_H) { # HEX @char=('a'..'f','0'..'9'); } elsif ($opt_m) { # Minimal @char=('a'..'z','0'..'9',',','.','/','-','=',';'); } elsif ($opt_a) { # Alphanumeric @char=('a'..'z','0'..'9'); } elsif ($opt_n) { # Numeric @char=('0'..'9'); } if ($opt_p) { print join('',@char),"\n"; exit(0); } $times=($#ARGV>=0 && $ARGV[0]>1)?$ARGV[0]:1; $chars=($#ARGV>=1 && $ARGV[1]>0)?$ARGV[1]:8; if ($opt_V) {$opt_v=1} elsif ($opt_v) {} else {$opt_v=0} # Initialize random number generator with decent numbers, hopefully not # guessable or replayable as the current time would be $i = eval 'use MD5; 1'; if ($i == 1) { $hash = new MD5; $hash->reset(); foreach $file ( @random_seeds ) { #print "Adding random data from: $file\n"; open(FILE,$file) || next; $hash->addfile(FILE); close(FILE); } $digest = $hash->digest(); $hexdigest = unpack("H*",$digest); #$hash->add( time ); foreach $value (unpack("I*",$digest)) { $init ^= $value; } } else { # Normal, low-entropy, time/PID initialization print "Warning: using low-entropy initializer!\n"; $init=time(); } $init ^= $$; #print "Initialized with: (", $hexdigest , ") => $init\n"; srand( $init ); #print "Generating $chars chars - $opt_v random chars\n"; for ($t=0;$t<$times;$t++) { $num= $chars - int(($opt_v+1)*rand); for ($i=0;$i<$num;$i++) {print $char[$#char*rand]} print "\n"; }