#!/usr/bin/perl #srand( time() ^ ($$ + ($$ << 15)) ); my $gVERSION = "1.4"; my $gAlpha = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"; my $gNumeric = "0123456789"; my $gSymbol = "\!\@\#\$\^\-\.\*"; my $gUserPattern = 0; my $gQuiet = 0; my $gRunTimes = 1; # how many passwords to generate my $gPopulation = "AAA000!!"; my $gChoice = int(rand($gNumPatterns)); my $gMyPattern = &GeneratePattern; &ParseCommandLine(@ARGV); die "\nMaximum pattern length is 8 characters\n\n" if (length($gMyPattern) > 8); for ($mastercount = 1; $mastercount < $gRunTimes + 1; $mastercount++) { $gMyPattern = &GeneratePattern if ($gUserPattern == 0); print "\n(" . $mastercount . "/" . $gRunTimes . ") " if ($gQuiet == 0); &GeneratePassword; } sub GeneratePassword { my $Password, $Hash, $Salt; for ($count = 0; $count < length($gMyPattern); $count++) { if (substr($gMyPattern,$count,1) eq "?") { $rand = int(rand(3)); substr($gMyPattern,$count,1) = substr("A0!",$rand,1); } if (substr($gMyPattern,$count,1) eq "A") { $Password = $Password . substr($gAlpha,int(rand(length($gAlpha))),1); next; } if (substr($gMyPattern,$count,1) eq "0") { $Password = $Password . substr($gNumeric,int(rand(length($gNumeric))),1); next; } if (substr($gMyPattern,$count,1) eq "!") { $Password = $Password . substr($gSymbol,int(rand(length($gSymbol))),1); next; } # We should never reach this point die "\n" . substr($gMyPattern,$count,1) . " - patterns may only contain A, 0 or ! (" . $gMyPattern . ")\n\n"; } $Salt = substr($gAlpha,int(rand(length($gAlpha))),1) . substr($gNumeric,int(rand(length($gNumeric))),1); $Hash = crypt($Password,$Salt); if ($gQuiet == 0) { print "Pattern: " . $gMyPattern . " Password: " . $Password . " Hash: " . $Hash . "\n\n"; } elsif ($gQuiet == 1) { print $Password . "\n"; } elsif ($gQuiet == 2) { print "$Password $Hash\n"; } } sub GeneratePattern { local $a, $r, $done, $count, $string; my (@Places); $string = "-" x length($gPopulation); for ($count = 0 ; $count < length($gPopulation); $count++) { $Places[$count] = $count; } for ($count = 0 ; $count < length($gPopulation); $count++) { $a = substr($gPopulation,$count,1); $r = int(rand(scalar(@Places))); substr($string,$Places[$r],1) = $a; splice(@Places,$r,1); } return($string); } sub ParseCommandLine { local @CommandLine = @_; local $count, $string; for ($count = 0; $count < @CommandLine; $count++) { $string = $CommandLine[$count]; $string =~ tr/A-Z/a-z/; if ($string eq "-h") { ## Version &ShowHelp; } elsif ($string eq "-q") { ## Quiet Mode, only print password $gQuiet = 1; } elsif ($string eq "-r") { ## Quiet Mode, only print hash $gQuiet = 2; } elsif ($string eq "-p") { ## User Supplied Password $gMyPattern = $CommandLine[$count+1]; $gUserPattern = 1; $count++; } elsif ($string eq "-t") { ## How many passwords to generate? $gRunTimes = $CommandLine[$count+1]; $count++; } else { print "\n** Unknown option: $string, use -h for help\n\n"; exit; } } } sub ShowHelp { print "passgen.pl (v. $gVERSION)\n\n"; print "passgen.pl [-h] [-q] [-p pattern] [-t num]\n"; print " -h : help\n"; print " -q : quiet mode ... only display password\n"; print " -r : quiet mode ... only display password & hash\n"; print " -p pattern : help\n"; print " -t num : generate passwords\n\n"; exit; }