-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_random_pw.pl
executable file
·164 lines (147 loc) · 3.01 KB
/
create_random_pw.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
#!/usr/bin/perl
# -------------------------------------------------------------
#
# Description: Create random password
#
#
# Revision History
# ================
#
# Date By Change
# ---------- ---- ---------------------------------------------
# 2013-06-13 mlsd Created
# -------------------------------------------------------------
use warnings;
use strict;
use List::Util 'shuffle';
use Getopt::Std;
my %s = (
# standard length of password
"std_length" => "12",
# Help message
"help_msg" =>
"usage: $0 [-h] [-l <number>] [-n <number>] [-s] [-d]
-h\t\t\t\thelp
-l <number>\tlenght of password (12 is default)
-s\t\t\t\tuse special characters (!, % or #)
-n <number>\tgenerate this many passwords
-d\t\t\t\tdescribe password
Examples:
$0
$0 -l 14
$0 -l 10 -d -s -n 10
$0 -l 9 -s\n",
);
# Input options
getopts('l:shdn:');
our ($opt_l, $opt_s, $opt_h, $opt_d, $opt_n);
# Print help message
if ( $opt_h ){
print $s{"help_msg"};
exit 2;
}
$opt_n = 1 if !$opt_n or ( $opt_n and $opt_n < 0 );
# all characters
my %characters = (
numbers => {
0 => "zero",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
},
words => {
a => "alfa",
b => "bravo",
c => "charlie",
d => "delta",
e => "echo",
f => "foxtrot",
g => "golf",
h => "hotel",
i => "india",
j => "juliett",
k => "kilo",
l => "lima",
m => "mike",
n => "november",
o => "oscar",
p => "pappa",
q => "quebec",
r => "romeo",
s => "sierra",
t => "tango",
u => "uniform",
v => "victor",
x => "xray",
y => "yankee",
z => "zulu",
},
WORDS => {
},
chars => {
"_" => "underscore",
"=" => "equivalent",
},
spec => {
"%" => "procent",
"!" => "exclamation",
"#" => "hashtag",
},
);
my (%newpw,%expl,$count);
my $length = $s{"std_length"};
$length = $opt_l if $opt_l;
# Generate passwords
map {
%newpw = ();
%expl = ();
$count = 0;
foreach (1..$length){
$count++;
my $do;
if ( $opt_s ){
$do = shuffle keys %characters;
} else {
$do = shuffle ("WORDS", "words", "numbers", "chars");
}
&gen_one_string($do);
}
&pw_print();
} 1..$opt_n;
sub gen_one_string {
my $do = shift;
my $is_uc = 1 if $do eq uc($do);
$do = lc($do) if $is_uc;
$newpw{$count} = "".shuffle keys %{$characters{$do}};
$expl{$count} = $characters{$do}{$newpw{$count}};
$newpw{$count} = uc($newpw{$count}) if $is_uc;
}
sub pw_print {
# print password
print map { $newpw{$_} } sort keys %newpw;
# print description, if wanted
if ( $opt_d ){
print "\t\t[ ";
my $string = "";
foreach my $e (sort keys %expl) {
# check for uppercase words
if ( $newpw{$e} =~ /[a-z]/i and $newpw{$e} eq uc($newpw{$e}) ){
$string .= uc($expl{$e})." - ";
}
else {
$string .= $expl{$e}." - ";
}
}
$string =~ s/- $//;
print "$string]\n";
}
else {
print "\n";
}
}