-
Notifications
You must be signed in to change notification settings - Fork 11
/
mkpass
107 lines (89 loc) · 3.31 KB
/
mkpass
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
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/mkpass
# Started On - Fri 20 Oct 12:14:09 BST 2017
# Last Change - Sat 7 Apr 14:26:50 BST 2018
# Author E-Mail - terminalforlife@yahoo.com
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
# Originally a ShellPlugin, called with shellpass, but has since been improved.
#------------------------------------------------------------------------------MAIN
_VERSION_="2018-04-07"
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
MKPASS ($_VERSION_)
Written by terminalforlife (terminalforlife@yahoo.com)
Pure Bourne Again Shell approach to complex password generation.
SYNTAX: mkpass [OPTS] INT
OPTS: --help|-h|-? - Displays this help information.
--version|-v - Output only the version datestamp.
--type|-t TYPE - Where T is the type. See below.
NOTE: This small program uses /dev/urandom to generate a wide range of
characters, just as you would expect from genpass, but fuller and
with extra features and choices for the user.
Where INT is the number (integer) of characters to generate.
TYPES: Available TYPEs:
+------------+----------------------+------------------+
| VALID TYPE | WHAT IT INCLUDES | REGEX FORM |
+------------+----------------------+------------------+
| punct | Punctuation | [[:punct:]] |
| digit | Digits | [[:digit:]] |
| alpha | Letters | [[:alpha:]] |
| alnum | Letters and Digits | [[:alnum:]] |
| alphapunct | All of the above. | [[:alphapunct:]] |
+-----------------------------------+------------------+
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--version|-v)
printf "%s\n" "$_VERSION_"
exit 0 ;;
--type|-t)
shift; TYPE="$1" ;;
-*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
*)
break ;;
esac
shift
done
if [[ "$1" =~ [0-9]+ ]]; then
INT=$1
elif [ -z "$INT" ]; then
XERR "$LINENO" "Invalid or missing INT."
elif ! [ -e /dev/urandom ]; then
XERR "$LINENO" "Unable to access /dev/urandom device."
elif ! [[ "$*" =~ [0-9]+ ]] && [ "$INT" -ge 1 ]; then
XERR "$LINENO" "Requires an integer greater than or equal to 1."
fi
case "${TYPE:-EMPTY}" in
punct)
CHARS='[\`/\+\\\|\_\,\.\?\!\"\\\/\$\%\^\&\*\(\)\{\}\]\[-\>\<\:\@\~]' ;;
digit)
CHARS='[0-9]' ;;
alpha)
CHARS='[A-Za-z]' ;;
alnum)
CHARS='[0-9A-Za-z]' ;;
alphapunct|EMPTY)
CHARS='[\`/\+\\\|0-9A-Za-z\_\,\.\?\!\"\\\/\$\%\^\&\*\(\)\{\}\]\[-\>\<\:\@\~]' ;;
*)
XERR "$LINENO" "Invalid TYPE selected." ;;
esac
declare -i A=0
while read -a X; do
if [[ "${X:0:1}" == $CHARS ]]; then
A+=1
printf "%s" "${X:0:1}"
[ $A -eq $INT ] && break
fi
done < /dev/urandom
printf "\n"
# vim: ft=sh noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup