Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Created January 25, 2011 09:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschmitt/794727 to your computer and use it in GitHub Desktop.
Save mschmitt/794727 to your computer and use it in GitHub Desktop.
Dictionary-based password generator in Perl
#!/usr/bin/perl -w
use strict;
use diagnostics;
# https://gist.github.com/794727
# Dictionary-based password generator in Perl
usage() if ($ARGV[0] and ($ARGV[0] eq '-?'));
my $wordlist = '/usr/share/dict/words';
my @chars = qw(- + . *);
my @words4;
open my $fh_in, "<$wordlist" or die "Can't open dictionary: $wordlist\n";
while(<$fh_in>){
chomp;
if (/^[abcdefghjkmnpqrstuvwxyz]{4}$/i){
push @words4, $_;
}
}
my @numbers;
foreach (0..999){
push @numbers, sprintf("%3.3d", $_);
}
foreach (1..20){
my $pass;
$pass .= $words4[rand @words4];
$pass .= $numbers[rand @numbers];
$pass .= $chars[rand @chars];
$pass .= $words4[rand @words4];
my $hash1 = `echo $pass | openssl passwd -crypt -stdin`;
chomp $hash1;
my $hash2 = `echo $pass | openssl passwd -1 -stdin`;
chomp $hash2;
if ($ARGV[0]){
my $output;
my @format = split //, $ARGV[0];
while(my $fmt = shift @format){
if ($fmt eq "p"){
$output .= $pass;
}elsif($fmt eq "c"){
$output .= $hash1;
}elsif($fmt eq "m"){
$output .= $hash2;
}else{
$output .= $fmt;
}
}
print "$output\n";
}else{
printf "%-15s %-15s %-40s\n", $pass, $hash1, $hash2;
}
}
sub usage{
print <<EOF;
betterpasswords
---------------
betterpasswords without arguments will give you:
<password> <cryptpw> <md5pw>
Use a format string to generate custom output.
Characters p, c and m will be replaced with password, cryptpw and md5pw,
respectively.
Example:
betterpasswords p
betterpasswords p:m
betterpasswords 'p -> c'
Copyright (c) 2011, Martin Schmitt <mas at scsy dot de>
EOF
exit;
}
__END__
Copyright (c) 2011, Martin Schmitt <mas at scsy dot de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment