-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-password.pl
executable file
·197 lines (144 loc) · 4.08 KB
/
gen-password.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/perl
use strict;
use warnings;
my ($help_screen);
$help_screen = << 'Endofblock';
This program generates a password using xkcd's algorithm (concatenating
several random dictionary words). This gives passwords that are strong and
easy to remember.
NOTE - This uses rand(), which is not cryptographically strong.
Usage: gen-password <options>
Options:
--help Prints this screen.
--dictionary=(file) Specifies dictionary; default is "/usr/share/dict/words".
--numwords=N Specifies number of words; default is 4.
--debug-dict Prints dictionary debugging information.
Endofblock
#
# Functions
# No arguments.
# Returns a pointer to a configuration hash.
sub ParseArgs
{
my ($config_p);
my ($aidx, $thisarg);
$config_p =
{ 'want_help' => 0, 'had_problem' => 0,
'numwords' => 4, 'want_dict_debug' => 0 };
if (-e '/usr/share/dict/words')
{ $$config_p{'dictionary'} = '/usr/share/dict/words'; }
elsif (-e '/etc/dictionaries-common/words')
{ $$config_p{'dictionary'} = '/etc/dictionaries-common/words'; }
for ($aidx = 0; defined ($thisarg = $ARGV[$aidx]); $aidx++)
{
if ( ($thisarg eq '--help') || ($thisarg eq '-?') )
{ $$config_p{'want_help'} = 1; }
elsif ($thisarg =~ m/^--dictionary=(.*)$/)
{ $$config_p{'dictionary'} = $1; }
elsif ($thisarg =~ m/^--numwords=(\d+)$/)
{ $$config_p{'numwords'} = $1; }
elsif ($thisarg eq '--debug-dict')
{ $$config_p{'want_dict_debug'} = 1; }
else
{
print
"### Unrecognized option \"$thisarg\". Use \"--help\" for help.\n";
$$config_p{'had_problem'} = 1;
}
}
if ($$config_p{'numwords'} < 1)
{ $$config_p{'numwords'} = 1; }
return $config_p;
}
# Arg 0 is the filename to read from.
# Returns a pointer to a list of dictionary words, filtered to remove
# various unwanted entries. Returns undef on error.
sub ReadDictionary
{
my ($dict_p, $fname);
my (@rawlist, @cookedlist, $thisword);
my ($skipcount);
$fname = $_[0];
$dict_p = undef;
if (!open(INFILE, "<$fname"))
{ print "### Couldn't read from \"$fname\"!\n"; }
else
{
@rawlist = <INFILE>;
close(INFILE);
@cookedlist = [];
$skipcount = 0;
foreach $thisword (@rawlist)
{
chomp($thisword);
# Get rid of apostrophes, suffixes, etc; there are a lot of these.
# Also words starting with capitals, short words, etc.
if ( (length($thisword) >= 5) && (length($thisword) <= 7)
&& (!($thisword =~ m/[^a-z]/)) && (!($thisword =~ m/s$/))
&& (!($thisword =~ m/ed$/)) && (!($thisword =~ m/ing$/))
&& (!($thisword =~ m/ly$/)) && (!($thisword =~ m/er$/)) )
{ push @cookedlist, $thisword; }
else
{ $skipcount++; }
}
if (scalar(@cookedlist) < 1000)
{
print "### Didn't find enough words in \"$fname\"!\n";
PrintDictionaryStats(\@cookedlist);
print "Filtered $skipcount words.\n";
}
else
{
$dict_p = [];
@$dict_p = @cookedlist;
}
}
return $dict_p;
}
# Arg 0 points to a list of words.
# No return value.
sub PrintDictionaryStats
{
my ($dict_p);
$dict_p = $_[0];
print "Dictionary contains " . scalar(@$dict_p) . " words.\n";
}
# Arg 0 points to a list of words.
# Arg 1 is the number of words to concatenate.
# Returns a concatenated word string.
sub GenPassword
{
my ($result, $dict_p, $wordcount);
my ($dictcount, $widx, $didx);
$dict_p = $_[0];
$wordcount = $_[1];
$result = '';
$dictcount = scalar(@$dict_p);
for ($widx = 0; $widx < $wordcount; $widx++)
{
$didx = int(rand($dictcount));
$result = $result . ucfirst($$dict_p[$didx]);
}
return $result;
}
#
# Main Program
my ($config_p, $dictlist_p);
$config_p = ParseArgs();
if ($$config_p{'want_help'})
{ print $help_screen; }
elsif (!($$config_p{'had_problem'}))
{
$dictlist_p = ReadDictionary($$config_p{'dictionary'});
if (defined $dictlist_p)
{
if ($$config_p{'want_dict_debug'})
{ PrintDictionaryStats($dictlist_p); }
else
{
print GenPassword($dictlist_p, $$config_p{'numwords'}) . "\n";
}
}
}
#
# This is the end of the file.