-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenpass.pl
executable file
·281 lines (250 loc) · 7.12 KB
/
genpass.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/perl
##
## Generates a random password (or some other key)
## Copyright (c) 2005,2007,2011,2013 by Michal Nazarewicz (mina86/AT/mina86.com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
use warnings;
use strict;
## Forward declarations
sub buildChars($);
sub randomBytes($);
sub encode($$);
sub transform($$$);
## Modifiers
my %modifiers = (
'hex' => '0123456789abcdef',
'HEX' => '0123456789ABCDEF',
'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'al' => '--alpha',
'alnum' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
'lower' => 'abcdefghijklmnopqrstuvwxyz',
'lw' => '--lower',
'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'up' => '--upper',
'b64' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/',
'all' => join('', map chr, 33..126),
'guard' => { 'prefix' => '/!' },
'noguard' => { 'prefix' => '' },
'g' => '--guard',
'G' => '--noguard',
'strength' => { 'strength' => 1 },
'nostrength' => { 'strength' => 0 },
's' => '--strength',
'S' => '--nostrength',
'clear' => { 'prefix' => '', 'strength' => 0 },
'c' => '--clear',
);
# Help
if (@ARGV == 1 && $ARGV[0] =~ /^(?:(?:--)?help|-h)$/) {
print <<'END';
usage: $0 [ <options> ... ] [ <length> ]
Choosing characters set:
hex Use lower case hexadecimal characters in the password.
HEX Use upper case hexadecimal characters in the password.
al alpha Use lower and upper case letters.
lw lower Use lower case letters.
up upper Use upper case letters.
* b64 Use characters used in base64 encoding (letters, digits,
plus and slash).
all Use all printable ASCII characters (codes from 33 to 126
inclusively).
ch:<spec> Custom character list.
Additional options:
* g guard Add "/!" at the beginning of generated password.
Slash prevents accidental pasting of the password
into IRC clients, and exclamation mark prevents
accidental pasting of the password on shell command
line. This prefix is not counted towards password length.
G noguard Do not add "/!" at the beginning of generated password.
* s strength Ensure password has one upper case letter, one lower
case letter, and one digit. This option causees
those characters to be added regardless of chosen
characters sets. This suffix is added to the password and
is not counted towards its length as specified by <length>.
S nostrength Disable the above behaviour.
c clear Synonym of "noguard nostrength".
<length> specifies desired password length. The default is to choose
a length such that the password has at least 80 bits of entropy.
* indicates default option.
END
}
# Parse arguments
my ($chars, $length, %O, $excess);
unshift @ARGV, qw(b64 guard strength);
do {
$_ = shift @ARGV;
if (/^\d+$/ && $_ != 0) {
$length = int $_;
} elsif (/^ch:/) {
$chars = buildChars substr $_, 3;
} elsif (!exists $modifiers{$_}) {
die "$_: unknown modifier\n";
} elsif ('HASH' eq ref $modifiers{$_}) {
my $modifier = $modifiers{$_};
for (keys %$modifier) {
$O{$_} = $modifier->{$_};
}
} elsif ($modifiers{$_} =~ /^--/) {
unshift @ARGV, substr $modifiers{$_}, 2;
} else {
$chars = $modifiers{$_};
}
} while (@ARGV);
# Do the work
if (defined $length) {
$_ = randomBytes $length + 3; # TODO
} else {
$_ = randomBytes 13;
}
$_ = encode $_, $chars;
if (defined $length) {
$excess = substr $_, $length;
$_ = $O{'prefix'} . substr $_, 0, $length;
} else {
$excess = substr $_, -3;
$_ = $O{'prefix'} . substr $_, 0, -3;
}
if ($O{'strength'}) {
if (m/[A-Z]/) {
$_ .= substr $excess, 0, 1;
} else {
$_ .= chr(ord('A') + int rand 26);
}
if (m/[a-z]/) {
$_ .= substr $excess, 1, 1;
} else {
$_ .= chr(ord('a') + int rand 26);
}
if (m/[0-9]/) {
$_ .= substr $excess, 2, 1;
} else {
$_ .= chr(ord('0') + int rand 10);
}
}
print $_, "\n";
## Generate chars
sub buildChars($) {
my (@chars, $prev, $range, $ch);
for $ch (unpack 'C*', $_[0]) {
if (defined $range) {
pop @chars;
if ($prev < $ch) {
push @chars, $prev..$ch;
} else {
die sprintf("%c-%c: invalid character range\n",
$prev, $ch);
}
undef $prev;
undef $range;
} elsif (defined $prev && $ch == ord '-') {
$range = 1;
} else {
push @chars, $ch;
$prev = $ch;
}
}
my $chars = '';
undef $prev;
for $ch (sort @chars) {
if (!defined $prev || $prev != $ch) {
$chars .= chr $ch;
}
$prev = $ch;
}
if (!length $chars) {
die "empty character specification\n";
} elsif (length $chars == 1) {
die "$chars: only one character specified\n";
}
$chars;
}
## Read random data
sub readRandomBytes($$);
sub genRandomBytes($);
sub randomBytes($) {
my $bytes = $_[0];
my $data =
readRandomBytes($bytes, '/dev/urandom') //
genRandomBytes($bytes);
substr $data, 0, $bytes;
}
sub readRandomBytes($$) {
my ($bytes, $file, $fd) = @_;
if (!open $fd, '<', $file) {
return;
}
my $data = '';
do {
$bytes -= read $fd, $data, $bytes, length $data;
} while ($bytes);
close $fd;
$data;
}
sub genRandomBytes($) {
warn "unable to open /dev/*random, will use week software generator\n";
my $bytes = $_[0];
my $data = '';
do {
my $num = int rand 0x10000;
$data .= chr($num % 256) . chr($num / 256);
$bytes -= 2;
} while ($bytes > 0);
$data;
}
## Encoder
sub transform($$$) {
my ($str, $from, $to) = @_;
if ($from ne $to) {
if (length $from != length $to) {
die "internal (<<$from>> <<$to>>)\n";
}
eval "\$str =~ tr[$from][$to], 1" or die $@;
}
$str;
}
sub encode($$) {
my ($bytes, $chars) = @_;
if (length $chars == 16) {
transform unpack('h*', $bytes), '0123456789abcdef', $chars;
} elsif (length $chars == 64) {
require MIME::Base64;
MIME::Base32->import('RFC');
my $str = MIME::Base64::encode($bytes);
chomp $str;
$str =~ s/=+$//;
transform $str, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/', $chars;
} else {
my $ret = '';
my $group_size = length $chars;
my ($val, $ent) = (0, 0);
for my $ch (unpack 'C*', $bytes) {
$val = ($val * 256) | $ch;
$ent = ($ent * 256) | 255;
while ($ent >= $group_size) {
$ret .= substr $chars, $val % $group_size, 1;
$ent = int $ent / $group_size;
$val = int $val / $group_size;
}
}
if ($ent > 0) {
$ret .= substr $chars, $val, 1;
}
return $ret;
}
}