-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_password.sh
executable file
·106 lines (84 loc) · 2.05 KB
/
create_password.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
#!/bin/bash
LOWERCASE="abcdefghijklmnopqrstuvwxyz"
UPPERCASE="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
NUMBERS="0123456789"
SPECIAL_CHARACTERS="~!@#\$%^&*()-_=+[]{}|;:',.<>/?\"\\"
if [[ $# -ne 5 ]]
then
echo "Usage: $0 <arg1> <arg2> <arg3> <arg4> <arg5>"
echo " arg1 : <allow_lowercase? 0 or 1>"
echo " arg2 : <allow_uppercase? 0 or 1>"
echo " arg3 : <allow_numbers? 0 or 1>"
echo " arg4 : <allow_special_characters? 0 or 1>"
echo " arg5 : <size? number>"
exit 1
fi
allow_lowercase=$1
allow_uppercase=$2
allow_numbers=$3
allow_special_characters=$4
size=$5
if [[ $allow_lowercase -ne 0 && $allow_lowercase -ne 1 ]]
then
echo "Error: allow_lowercase must be 0 or 1"
exit 1
fi
if [[ $allow_uppercase -ne 0 && $allow_uppercase -ne 1 ]]
then
echo "Error: allow_uppercase must be 0 or 1"
exit 1
fi
if [[ $allow_numbers -ne 0 && $allow_numbers -ne 1 ]]
then
echo "Error: allow_numbers must be 0 or 1"
exit 1
fi
if [[ $allow_special_characters -ne 0 && $allow_special_characters -ne 1 ]]
then
echo "Error: allow_special_characters must be 0 or 1"
exit 1
fi
regex='^[0-9]+$'
if ! [[ $size =~ $regex ]]
then
echo "Error: size must be a number"
exit 1
fi
if [[ $allow_lowercase -eq 0 && $allow_uppercase -eq 0 && $allow_numbers -eq 0 && $allow_special_characters -eq 0 ]]
then
echo "Error: no characters are allowed... nothing can be generated"
exit 1
fi
if [[ $size -lt 1 ]]
then
echo "Error: size is less than 1... nothing can be generated"
exit 1
fi
char_set=""
if [[ $allow_lowercase -eq 1 ]]
then
char_set="${char_set}$LOWERCASE"
fi
if [[ $allow_uppercase -eq 1 ]]
then
char_set="${char_set}$UPPERCASE"
fi
if [[ $allow_numbers -eq 1 ]]
then
char_set="${char_set}$NUMBERS"
fi
if [[ $allow_special_characters -eq 1 ]]
then
char_set="${char_set}$SPECIAL_CHARACTERS"
fi
char_set_length=${#char_set}
code=""
for i in `seq 1 $size`
do
index=$(($RANDOM % $char_set_length))
char=${char_set:$index:1}
code="$code$char"
done
echo "$code" | pbcopy
echo "Your clipboard now contains the randomly generated passcode"
exit 0