#!/usr/bin/perl # # Copyright (c) 2007, Gregory Fleischer (gfleischer@gmail.com) # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # 3. The names of the authors may not be used to endorse or promote # products derived from this software without specific prior # written permission. # # THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # my @special_chars = qw[! @ $ % ^ & * ( ) { } \ | + = - _ , . / ? : ; ]; my @chars = ('a'..'z', 'A'..'Z', '0'..'9'); my @args = (); my $special = 0; foreach my $arg (@ARGV) { if ($arg=~/^--?special$/) { $special = 1; } elsif ($arg=~/^--?s(?:pecial)?=(.+)$/) { $special = 1; @special_chars=split(//, $1); } elsif ($arg=~/^--?c(?:harset)?=(alpha|alnum|digit|lower|upper)$/) { if ("alpha" eq $1) { @chars= ('a'..'z','A'..'Z'); } elsif ("alnum" eq $1) { @chars = ('a'..'z', 'A'..'Z', '0'..'9'); } elsif ("lower" eq $1) { @chars = ('a'..'z'); } elsif ("upper" eq $1) { @chars = ('A'..'Z'); } elsif ("digit" eq $1) { @chars = ('0'..'9'); } else { die "unexpected charset: $1"; } } elsif ($arg=~/^--?h(elp)?$/) { &help(); exit(0); } elsif ($arg=~/^\d+$/) { push(@args, $arg); } else { print "invalid option: $arg\n"; &help(); exit(0); } } my $wanted = shift(@args) || 8; if ($wanted !~ /^\d+$/) { die "bad number of chars wanted: $wanted"; } open(FH, '<', '/dev/urandom') or die "failed to open /dev/urandom: $!"; if ($special) { push(@chars, @special_chars); } my ($random, $this); my $needed = $wanted; while ((read(FH, $this, 1)) > 0) { my $i = ord($this) % ($#chars + 1); $random .= $chars[$i]; if (0 == --$needed) { last; } } if ($needed != 0) { die "entropy failed us!"; } print "$random\n"; exit(0); sub help { print "generate desired number of random characters (default 8)\n"; print "usage: $0 [options] [count]\n"; print " --chars= character set: alpha|alnum|digit (default alnum)\n"; print " --special include special characters:\n"; print " @special_chars\n"; print " --special= list of special characters\n"; print "\n"; } # eof