-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpg-urandom.sh
182 lines (151 loc) · 3.31 KB
/
rpg-urandom.sh
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
#!/usr/bin/env bash
scriptName="${0##*/}"
help_message() {
printf "
$scriptName <length>
$scriptName <options> <length>
Options:
[ -c <num> | --count <num> ] = Generate N number of passwords
[ -d <num> | --range <num> ] = Specify character range (see below)
[ -f | --fast ] = Generate fast but less secure passwords
[ -r | --random ] = Use /dev/random (Default: /dev/urandom)
[ -h | --help ] = Display this help message
Character ranges:
1 = Hexadecimal
1o = Hexadecimal (lowercase)
1u = Hexadecimal (uppercase)
2 = Alphabetic
2u = Alphabetic (uppercase)
2o = Alphabetic (lowercase)
3 = Alphanumeric
3o = Alphanumeric (lowercase)
3u = Alphanumeric (uppercase)
4 = ASCII (Default)
Examples:
Generate a 64-character length password
$scriptName 64
Generate 100 hexadecimal (lowercase) passwords of 64-character length
$scriptName -d1o -c100 64
Generate a 64-character password using /dev/random instead of /dev/urandom
$scriptName -r 64
"
}
ARGS=$(getopt -n random_password_generator -l random,fast,count:,range:,help -o rfc:d:h -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
'-r' | '--random')
random_type='/dev/random'
shift
;;
'-f' | '--fast')
gen_type=1
shift
;;
'-c' | '--count')
re='^[0-9]+$'
if ! [[ $2 =~ $re ]] ; then
printf "error: Not a number\n" >&2; exit 1
else
total_count="$2"
fi
shift 2
;;
'-d' | '--range')
charset_range="$2"
case $2 in
'1' | '1o')
#hexadecimal (lowercase, default)
charset_range='0-9a-f'
;;
'1u')
#hexadecimal (uppercase)
charset_range='0-9A-F'
;;
'2')
#alphabetic
charset_range='a-zA-Z'
;;
'2u')
#alphabetic (uppercase)
charset_range='A-Z'
;;
'2o')
#alphabetic (lowercase)
charset_range='a-z'
;;
'3')
#alphanumeric
charset_range='[:alnum:]'
;;
'3o')
#alphanumeric (lowercase)
charset_range='0-9a-z'
;;
'3u')
#alphanumeric (uppercase)
charset_range='0-9A-Z'
;;
'4')
#base94 (all chars on keyboard)
charset_range='[:graph:]'
;;
*)
printf "Option '-d${2}' does not exist. Aborting\n"
exit
esac
shift 2
;;
'-h' | '--help')
help_message && exit 0
shift
;;
--)
shift
break
;;
esac
done
## Empty arguments check
if [[ -z $@ ]]; then
help_message && exit 1
fi
if [[ -z $password_length ]]; then
re='^[0-9]+$'
if ! [[ ${@:1:1} =~ $re ]] ; then
printf "Password length should be number. Aborting.\n" >&2; exit 1
else
password_length="${@:1:1}"
fi
fi
if [[ -z $random_type ]]; then
random_type='/dev/urandom'
fi
if [[ -z $charset_range ]]; then
charset_range='[:alnum:]'
fi
if [[ -z $total_count ]]; then
total_count=1
fi
## Functions
gen_password() {
if (( gen_type == 1 )); then
cat "$random_type" \
| tr -dc "$charset_range" \
| head -c $(( password_length * total_count ))
else
for (( current_count = 0; current_count < total_count; current_count ++ )); do
cat "$random_type" \
| tr -dc "$charset_range" \
| head -c "$password_length"
done
fi 2> /dev/null
}
## Condition checks
if (( total_count > 1 )); then
gen_password | fold -w "$password_length"
printf '\n'
else
gen_password
printf '\n'
fi