-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpassword_generator.sh
90 lines (76 loc) · 2.2 KB
/
password_generator.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
#!/bin/bash
## Instead of handling user inputs via `read`, this script handles user inputs via input options
#read -p 'Type the length of the password: ' LENGTH
## Print manual while any error occurs
usage() {
echo 'Generate a random password'
echo ' -l, --length LENGTH Specify a password length'
echo ' -s Append a special character to the password'
echo ' -v, --verbose Increase verbosity'
exit 1
}
log() {
local MESSAGE="${@}"
if [[ "${VERBOSE}" = 'true' ]]
then
echo "${MESSAGE}"
fi
}
## Set a default password length
LENGTH=48
USE_SPECIAL_CHARACTERS='false'
## Define special characters
SPECIAL_CHARACTERS="!@#$%^&*()_-+="
## Input options (vl:s means the options must be followed with an argument)
while getopts vl:s OPTION
do
case ${OPTION} in
h)
usage
;;
v)
VERBOSE='true'
log 'Verbose mode: [on]'
;;
l)
LENGTH="${OPTARG}"
;;
s)
USE_SPECIAL_CHARACTERS='true'
;;
?)
echo "Usage: ${0} [-vs] [-l LENGTH]" >&2
usage
;;
esac
done
log "Number of args: [${#}]"
log "All args: [${@}]"
## Remove the options while remaining arguments
shift "$(( OPTIND - 1 ))"
if [[ "${#}" -gt 0 ]]
then
usage
fi
log '> Generating a password...'
if [[ "${USE_SPECIAL_CHARACTERS}" = 'true' ]]
then
log '> Selecting a random special character...'
## Generate a random special character
SPECIAL_CHAR=$(echo "${SPECIAL_CHARACTERS}" | fold -b1 | shuf | head -n1)
## Generate a 8-character password based on the sha256sum of nanoseconds and random numbers in conjunction
PASSWORD=$(date +%s%N${RANDOM}${RANDOM} | sha256sum | head -c${LENGTH-1})
else
## Generate a 8-character password based on the sha256sum of nanoseconds and random numbers in conjunction
PASSWORD=$(date +%s%N${RANDOM}${RANDOM} | sha256sum | head -c${LENGTH})
fi
log '> Done.'
log 'Here is the password:'
if [[ "${USE_SPECIAL_CHARACTERS}" = 'true' ]]
then
echo "${PASSWORD}${SPECIAL_CHAR}"
else
echo "${PASSWORD}"
fi
## Notify user that this script is done successfully
exit 0