-
Notifications
You must be signed in to change notification settings - Fork 8
/
safepw.sh
51 lines (46 loc) · 1.53 KB
/
safepw.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
#! /bin/bash
# $author: twfcc@twitter
# $PROG: safepw.sh
# $description: password generator for gfw.press
# $Usage : $0 8-99 ,default length 16 if argument is not given
# Public Domain use as your own risk
#
##################################################################
# rule of gfw.press password requirment: #
# 1) length at least 8, #
# 2) 1 or more uppercase letter, 1 or more lowercase letter and #
# 1 or more digit number must be included #
##################################################################
howmany=${1:-16}
password_gen(){
local matrix pw count pick i
matrix="123456789aAbBcCdDeEfFgGhHiIjJkKLmMnNpPqQrRsStTuUvVwWxXyYzZ"
count="${#matrix}"
for ((i=1 ; i<=howmany ;i++)) ; do
pick=${matrix:$((RANDOM%count-1)):1}
pw="$pw$pick"
done
echo "$pw"
}
case "$howmany" in
8|9) ;;
[1-9][0-9]) ;;
*) echo "Usage: $0 8-99" >&2
echo "Default length: 16 if no argument is given." >&2
exit 1
;;
esac
pass=$(password_gen)
while true ; do
lower=${pass//[!a-z]/}
upper=${pass//[!A-Z]/}
digit=${pass//[!0-9]/}
if [ -n "$lower" ] && [ -n "$upper" ] && [ -n "$digit" ]
then
break
else
unset pass
pass=$(password_gen)
fi
done
echo "$pass"