#!/usr/bin/perl
#randpass, psuedo random password generator
#Written by Pete Iriarte aka Coldhak
use strict; #why not?
use warnings; #might need it :)
sub usage {
print <DATA> and exit
}
&usage if !@ARGV;
&usage if $ARGV[0] =~ /[^0-9]/;#check for wierd commandline options
sub getrand { #rand() can only be used 1000 times before it isn't random. this fixes that little problem :)
my $randstr="perl -e 'print rand(".$_[0].")'";
$randstr =~ s/'/"/g if $^O =~ /win/i;
return `$randstr`;
}
my %charsets = ( #define the character sets to be used.
'nums' => '1234567890',
'uc' => join('','A'..'Z'),#upper case
'lc' => join('','a'..'z'),#lower case
'hex' => join('',('0'..'9','A'..'F')),
'bin' => '10',
'alpha' => join('',('A'..'Z','a'..'z')),
'hrc' => join('',('a'..'z','A'..'Z',0..9)),#(human readable characters/alphanumeric)
'default' => join('',('a'..'z','A'..'Z','0'..'9',q(()*&^%$#@!{}[]'";,.><?/|\\/`+=-_~))),
'all' => join('',map chr,1..255)
);
$ARGV[1] = 'default' if !defined $ARGV[1];
my @chars;
if($ARGV[1] =~ /^-C:(.+)/i) {
@chars = split('',$1);
}
else {
@chars = split('',$charsets{lc($ARGV[1])}) or die "Charset ".$ARGV[1]." does not exist.";
}
my $length = int($ARGV[0]);
my $i = 0;
while($i < $length) {
my $randnum;
if($i < 1000) {
$randnum=rand(scalar @chars);#saves time
}
else {
$randnum=&getrand(scalar @chars);
}
print($chars[int $randnum]);
$i++;
}
__DATA__
###############################
# Randpass written by Coldhak #
###############################
randpass length [-C:stuff | charset]
length - the length of the password you want. use [0-9]+
charsets - num, hex, bin, uc, lc, alpha, hrc, all, default
-C:stuff - here -C means custom charset, and the stuff after the : is the charset.