Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Generic Password Generator

by claree0 (Hermit)
on Jun 28, 2001 at 15:35 UTC ( #92246=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info Clare Elliott - celliott@star.net.uk
Description: A simple (i.e. easily adapted by those with more specific requirements) password generator, which will generate passwords according to a template. Useful for environments where the use of a specific format of password is encouraged.
#!/usr/bin/perl
 
$numargs = scalar @ARGV;
 
if ($numargs == 0) {
        print "Clare's Random Password Generator\n";
        print "Usage: pwdgen template [number]\n";
        print "where template is a string composed of the following ch
+aracters\n";
        print "\tl\tlower case letter\n\tL\tupper case letter\n\tc\tlo
+wer case consonant\n\tC\tupper case consonant\n";
        print "\tl\tlower case vowel\n\tL\tupper case vowel\n\tn\tnumb
+er\n\tp\tpunctuation mark\n\ta\tany character\n";
        print "Any character in the template which is not one of those
+ above will be placed in the password at the same position\n";
        print "number refers to the number of passwords to generate - 
+this defaults to 10.\n";
}
 
$template = $ARGV[0];
 
if ($ARGV[1] =~ /^\d+$/) {
        $num = $ARGV[1];
}
else {
        $num = 10;
}
 
my @l = split '', 'qwertyuiopasdfghjklzxcvbnm';
my @n = split '', '0123456789';
my @p = split '', ',.?<>;:@/!"%^&*()-+=_';
my @L = split '', 'QWERTYUIOPASDFGHJKLZXCVBNM';
my @c = split '', 'qwrtypsdfghjklzxcvbnm';
my @C = split '', 'QWRTYPSDFGHJKLZXCVBNM';
my @v = split '', 'aeiou';
my @V = split '', 'AEIOU';
my @a = split '', 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBN
+M,.?<>;:@/!"%^&*()-+=_0123456789';
 
my @t = split '', $template;
$len = scalar @t;
 
my $newpass = "";
 
for ($c = 0; $c < $num; $c++) {
        for ($x = 0; $x < $len ; $x ++) {
                if ($t[$x] eq "l") { $newpass .= $l[rand @l]; next;}
                if ($t[$x] eq "L") { $newpass .= $L[rand @L]; next;}
                if ($t[$x] eq "c") { $newpass .= $c[rand @c]; next;}
                if ($t[$x] eq "C") { $newpass .= $C[rand @C]; next;}
                if ($t[$x] eq "v") { $newpass .= $v[rand @v]; next;}
                if ($t[$x] eq "V") { $newpass .= $V[rand @V]; next;}
                if ($t[$x] eq "n") { $newpass .= $n[rand @n]; next;}
                if ($t[$x] eq "p") { $newpass .= $p[rand @p]; next;}
                if ($t[$x] eq "a") { $newpass .= $a[rand @a]; next;}
                $newpass .= $t[$x];
        }
        print "$newpass\n";
        $newpass = "";
}

Comment on Generic Password Generator
Download Code
Re: Generic Password Generator
by mexnix (Pilgrim) on Jun 28, 2001 at 22:26 UTC
    I liked your program, but IMHO you might want to make the password generator a subroutine that only runs if $template is defined. I'm running on a windows box at work, and I didn't get to see the instructions when the program prints 10 blank lines at the bottom. Also I'm sure everyone would agree on use strict and -w. Just a thought.

    ...... # your code my $newpass = ""; sub make_passwords { for ($c... # your for loop } &make_passwords if $template;
    __________________________________________________
    %mexnix = (email = > "mexnix@hotmail.com", website => "http://mexnix.perlmonk.org");
Re: Generic Password Generator
by dooberwah (Pilgrim) on Jun 29, 2001 at 07:04 UTC
    First of all I'd just like to say that I think your code is really cool and that this program was a neat idea. I just made a few cosmetic changes to the code, please don't take any offense at this.

    Things I Changed

  • Made it so that it compiles with -w and use strict.
  • Put an exit(0) command so that it doesn't print out 10 blank lines with the message if no arguments are supllied.
  • Added code to check if $ARGV[1] is supplied so that it doesn't print a message about an empty regular-expression.

    The (Slightly) Changed Code:

    #!/usr/bin/perl -w use strict; my $numargs = scalar @ARGV; if ($numargs == 0) { print "Clare's Random Password Generator\n"; print "Usage: pwdgen template [number]\n"; print "where template is a string composed of the following ch +aracters\n"; print "\tl\tlower case letter\n\tL\tupper case letter\n\tc\tlo +wer case consonant\n\tC\tupper case consonant\n"; print "\tl\tlower case vowel\n\tL\tupper case vowel\n\tn\tnumb +er\n\tp\tpunctuation mark\n\ta\tany character\n"; print "Any character in the template which is not one of those + above will be placed in the password at the same position\n"; print "number refers to the number of passwords to generate - +this defaults to 10.\n"; exit(0); } my $num; my $template = $ARGV[0]; if (defined $ARGV[1]) { if ($ARGV[1] =~ /^\d+$/) { $num = $ARGV[1]; } } else { $num = 10; } my @l = split '', 'qwertyuiopasdfghjklzxcvbnm'; my @n = split '', '0123456789'; my @p = split '', ',.?<>;:@/!"%^&*()-+=_'; my @L = split '', 'QWERTYUIOPASDFGHJKLZXCVBNM'; my @c = split '', 'qwrtypsdfghjklzxcvbnm'; my @C = split '', 'QWRTYPSDFGHJKLZXCVBNM'; my @v = split '', 'aeiou'; my @V = split '', 'AEIOU'; my @a = split '', 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBN +M,.?<>;:@/!"%^&*()-+=_0123456789'; my @t = split '', $template; my $len = scalar @t; my $newpass = ""; for (my $c = 0; $c < $num; $c++) { for (my $x = 0; $x < $len ; $x ++) { if ($t[$x] eq "l") { $newpass .= $l[rand @l]; next;} if ($t[$x] eq "L") { $newpass .= $L[rand @L]; next;} if ($t[$x] eq "c") { $newpass .= $c[rand @c]; next;} if ($t[$x] eq "C") { $newpass .= $C[rand @C]; next;} if ($t[$x] eq "v") { $newpass .= $v[rand @v]; next;} if ($t[$x] eq "V") { $newpass .= $V[rand @V]; next;} if ($t[$x] eq "n") { $newpass .= $n[rand @n]; next;} if ($t[$x] eq "p") { $newpass .= $p[rand @p]; next;} if ($t[$x] eq "a") { $newpass .= $a[rand @a]; next;} $newpass .= $t[$x]; } print "$newpass\n"; $newpass = ""; }

    -Ben Jacobs (dooberwah)
    one thing i can tell you is you got to be free
    http://dooberwah.perlmonk.org

      Thank you - all good points.....

      Clare
        Hi, just one thing ... line 11 needs to be revised as follows:

        print "\tv\tlower case vowel\n\tV\tupper case vowel\n\tn\tnumb +er\n\tp\tpunctuation mark\n\ta\tany character\n";

        Otherwise your default ($numargs == 0) is showing "l" & "L" for vowels as well as letters.

        Other than that ... I like this ... I'm keeping it!

        So Long
        blackstarr

Really bad code.
by Anonymous Monk on Dec 03, 2007 at 13:46 UTC
    Clare, I hope you've improved in the six years since you wrote the above. Here's a version that isn't so newbie-ish.
    #!/usr/bin/perl use warnings; use strict; unless (@ARGV) { print <<"END_HELP"; Clare's Random Password Generator Usage: pwdgen template [number] where template is a string composed of the following characters: \tl\tlower case letter\n\tL\tupper case letter\n\tc\tlower case conson +ant\n\tC\tupper case consonant \tv\tlower case vowel\n\tV\tupper case vowel\n\tn\tnumber\n\tp\tpunctu +ation mark\n\ta\tany character Any character in the template which is not one of those above will be +placed in the password at the same position. [number] refers to the number of passwords to generate - this defaults + to 10. END_HELP exit(0); } my $count; my $template = $ARGV[0]; if ($ARGV[1] && $ARGV[1] =~ /^\d+$/) { $count = $ARGV[1]; } else { $count = 10; } my @numbers = (0..9); my @punctuation = qw(. ? < > : @ / ! " % ^ & * ( ) - + = _); my @loweralpha = ('a'..'z'); my @upperalpha = ('A'..'Z'); my @lowerconsonants = qw(b c d f g h j k l m n p q r s t v w x y z); my @upperconsonants = qw(B C D F G H J K L M N P Q R S T V W X Y Z); my @lowervowels = qw(a e i o u); my @uppervowels = qw(A E I O U); my @all = (@loweralpha, @upperalpha, @numbers, @punctuatio +n); my $symbols = { 'n' => \@numbers, 'p' => \@punctuation, 'l' => \@loweralpha, 'L' => \@upperalpha, 'c' => \@lowerconsonants, 'C' => \@upperconsonants, 'v' => \@lowervowels, 'V' => \@uppervowels, 'a' => \@all, }; for (0..$count) { generate_password(); } sub generate_password { my $password = ''; $password = join '', map { exists $symbols->{$_} ? $symbols->{$_}[rand @{$symbols->{$_} +}] : $_ } split //, $template; print "$password\n"; }
      Of course I managed to introduce a bug. That should have been for (1..$count)

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://92246]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-07-12 01:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls