forked from mdscunningham/shell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genpass.sh
executable file
·76 lines (62 loc) · 1.97 KB
/
genpass.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
#!/bin/bash
# +----+----+----+----+
# | | | | |
# Author: Mark David Scott Cunningham | M | D | S | C |
# +----+----+----+----+
# Created: 2016-10-10
# Updated: 2018-02-15
#
# Purpose: Quickly and simply generate random, secure passwords
#
usage(){
echo "
Usage: $(basename $0) [OPTIONS] [ARGUMENTS]
-l ... <length> in characters
-m ... Use NIST mkpasswd
-b ... Use OpenSSL Rand Base64
-x ... Use OpenSSL Rand Hex
-k ... Use xkcd.com/936/ method
"; exit 1
}
if [[ $@ =~ --help ]]; then usage; fi
length=24
method="xkcd"
while getopts l:kmxbh option; do
case "${option}" in
l) length=${OPTARG} ;;
k) method="xkcd" ;;
m) method="mkpasswd" ;;
x) method="hex" ;;
b) method="base64" ;;
h|*) usage ;;
esac
done
case $method in
mkpasswd )
if [[ $(grep NIST $(which mkpasswd)) ]]; then mkpasswd -l ${length}
else echo "You do not appear to have the NIST mkpasswd installed."; fi
;;
base64 )
openssl rand -base64 ${length} | tr -d '\n' | sed 's/ //g' | cut -c 1-${length}
;;
hex )
openssl rand -hex ${length} | cut -c 1-${length}
;;
xkcd )
wordList='/usr/share/dict/words';
if [[ -x /usr/bin/shuf ]]; then
wordLength=$(( (${length} - 4) / 4 ))
echo $(shuf -n 1000 $wordList | grep -E ^[a-z]{$wordLength}$ | shuf -n 4 )$(( ($RANDOM % 9000) + 1000 ))\
| sed 's/\b\(.\)/\u\1/g' | sed 's/ //g' | cut -c 1-${length}
else
n=0; word=(); len=$(wc -l < $wordList)
while [[ $n -lt 4 ]]; do
rnd=$(( $(od -vAn -N4 -tu4 < /dev/urandom) % $len + 1 ));
word[$n]=$(sed -n "${rnd}p" $wordList | grep -E '^[a-z]{4,8}$' | sed 's:\b\(.\):\u\1:');
if [[ -n ${word[$n]} ]]; then ((n++)); fi;
done;
echo "${word[0]}${word[1]}${word[2]}${word[3]}$(( $RANDOM % 9000 + 1000 ))";
unset n word len
fi
;;
esac