#!/usr/bin/perl # # randstring.pl - Generates a random string from a pattern # Copyright (C) 1998 Steven Pritchard # # This program is free software; you can redistribute it # and/or modify it under the same terms as Perl itself. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. use strict; use vars qw($foo $string $rand @upper @lower @digit @punct @any @salt); use English; # For readability use FileHandle; use String::Random; $rand=new String::Random; if (!defined($rand)) { print STDERR "Failed to create new object!\n"; exit 1; } $string=shift; if (!$string) { &usage($rand); exit 0; } &seed; print $rand->from_pattern($string), "\n"; sub usage { my($rand)=@_; my @foo; print STDERR < Characters in pattern must be one of the following: c Any lowercase character [@{ [ &x(join("",@{$rand->{"c"}})) ] }] C Any uppercase character [@{ [ &x(join("",@{$rand->{"C"}})) ] }] n Any digit [@{ [ &x(join("",@{$rand->{"n"}})) ] }] ! A punctuation character [@{ [ &x(join("",@{$rand->{"!"}})) ] }] . Any of the above s A "salt" character [@{ [ &x(join("",@{$rand->{"s"}})) ] }] END } sub x { my($string)=@_; my $upper=join("", ('A'..'Z')); my $lower=join("", ('a'..'z')); my $digit=join("", ('0'..'9')); $string=~s/$upper/A-Z/; $string=~s/$lower/a-z/; $string=~s/$digit/0-9/; return $string; } sub basename { my($name)=@_; return $name=~/\// ? substr($name,(-1*length($name))+(rindex($name, "/")+1)) : $name; } sub seed { if ($OSNAME eq "linux") # Should test for other systems with /dev/*random { # Read our seed from /dev/urandom my $urandom=new FileHandle "