John Stanish’s Code Examples

January 28, 2008

Random Password Generator

Filed under: Uncategorized — John Stanish @ 4:34 pm
  1: #!/usr/bin/perl
  2:
  3:
  4: #
  5: # Name:      Random password generator
  6: # Author:    John D. Stanish
  7: # Copyright: Copyright (C) 2002-2008, John D. Stanish, All rights reserved
  8: #
   :
  9: use strict;
 10: use warnings;
 11: use Getopt::Long;
   :
 12: #
 13: #  Get startup options from the command line
 14: #
 15: my $LENGTH        = 12;
 16: my $N             = 10;
 17: my $COLUMNS       = 4;
 18: my $ALTERNATECASE = 0;
 19: my $HELP          = 0;
 20: my $VERSION       = 0;
 21: my $CLASS         = 7;
 22: GetOptions(
 23:     'help'      => \$HELP,
 24:     'length=i'  => \$LENGTH,
 25:     'n=i'       => \$N,
 26:     'columns=i' => \$COLUMNS,
 27:     'version'   => \$VERSION,
 28:     'class=i'   => \$CLASS,
 29: );
   :
 30: #
 31: #  Print help information and quit
 32: #
 33: if ($HELP)
 34: {
 35:     print $0, " - generate random passwords\n";
 36:     print "\t--length    = password length (default: 12)\n";
 37:     print "\t--n         = number of passwords to generate (default: 10)\n";
 38:     print "\t--columns   = number of columns across to display (default: 4)\n";
 39:     print "\t--class     = type of password\n";
 40:     print "\t              1 = Uppercase alphabetic\n";
 41:     print "\t              2 = Lowercase alphabetic\n";
 42:     print "\t              3 = Alphabetic\n";
 43:     print "\t              4 = Numeric\n";
 44:     print "\t              7 = Alphanumeric\n";
 45:     print "\t              8 = Special symbols\n";
 46:     print "\n";
 47:     exit;
 48: }
   :
 49: #
 50: #  Print version information and quit
 51: #
 52: if ($VERSION)
 53: {
 54:     print $0, " - generate random passwords\n";
 55:     print "\n";
 56:     print "version 2.1 (November 11, 2006 9:51 AM)\n";
 57:     exit;
 58: }
 59: use POSIX qw/floor/;
 60: use Benchmark;
   :
 61: my $timestart = new Benchmark;
   :
 62: #
 63: # Build list of characters to use for passwords
 64: #
 65: my @passwords = ();
 66: my @chars = ();
 67: push @chars, 'A' .. 'Z'
 68:     if $CLASS & 1;
 69: push @chars, 'a' .. 'z'
 70:     if $CLASS & 2;
 71: push @chars, '0' .. '9'
 72:     if $CLASS & 4;
 73: push @chars, qw{ ! # , @ $ % ^ & * ( ) - = + [ ] | : ; . / ? < > }
 74:     if $CLASS & 8;
 75: for my $i ( 1 .. $N ) {
 76:     my $password  = "";
 77:     my %usedchars = ();
 78:     for ( 1 .. $LENGTH ) {
 79:         my $char;
 80:         while (1) {
 81:             $char = $chars[ rand( scalar(@chars) ) ];
 82:             $usedchars{$char}++;
 83:             last if $usedchars{$char} <= 1;
 84:         }
 85:         $password .= $char;
 86:     }
 87:     push @passwords, $password;
 88: }
   :
 89: #
 90: # Print passwords in a table format
 91: #
 92: my $offset = floor( $N / $COLUMNS ) + ( $N % $COLUMNS ? 1 : 0 );
 93: for my $i ( 0..$offset-1 )
 94: {
 95:     for my $j ( 0..$COLUMNS-1 )
 96:     {
 97:         my $index = $i + $j * $offset;
 98:         print $index + 1 . "\t" . $passwords[$index] . "\t\t"
 99:           if defined $passwords[$index];
100:     }
101:     print "\n";
102: }
103: print "\n";
   :
104: #
105: #  Print benchmark timing
106: #
107: my $timestop = new Benchmark;
108: my $timediff = timediff( $timestop, $timestart );
109: print timestr( $timediff, 'all' ), "\n";

The WordPress Classic Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.