-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg.sh
executable file
·183 lines (163 loc) · 4.17 KB
/
pg.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env bash
#
# Generate password from a string and a file
#
#/ Usage:
#/ ./pg.sh [-s|-c|-x|-p] [<length>]
#/
#/ Options:
#/ length Optional, must be an integer inside range (0, 105]
#/ Default length is 29
#/ -s Optional, show result in terminal only
#/ Default not show result, copy result to clipboard
#/ -c Optional, make first letter uppercase
#/ -x Optional, output hex chars
#/ Default base64 chars
#/ -p Optional, generate PIN with only numbers
#/ -h | --help Show usage message
set -e
set -u
usage() {
printf "%b\n" "$(grep '^#/' "$0" | cut -c4-)" && exit 1
}
set_var() {
[[ -z "${PG_FILE:-}" ]] && PG_FILE="$0"
_SEPERATOR="$"
_SALT=$(sha512sum "$PG_FILE" | cut -d' ' -f 1)
}
hash() {
# $1: string
# $2: length
# $3: first letter uppercase, true or false
local h
if [[ "${_HEX_OUT:-}" == true ]]; then
h=$(echo -n "$1" \
| sha512sum \
| sed -E 's,.....,&'"$_SEPERATOR"',g' \
| cut -c-"$2")
elif [[ "${_PIN_OUT:-}" == true ]]; then
h=$(echo -n "$1" \
| sha512sum \
| sed -E 's/[^[:digit:]]//g' \
| cut -c-"$2" )
else
h=$(echo -n "$1" \
| sha512sum \
| xxd -r -p \
| base64 \
| tr '\n' ',' \
| sed 's/,$/\n/;s/,//g' \
| sed -E 's,.....,&'"$_SEPERATOR"',g' \
| cut -c-"$2")
fi
if [[ "${3:-}" == true ]]; then
local l
l=$(get_first_letter "$h")
if [[ -n "$l" ]]; then
local i
i=$(get_str_index "$h" "$l")
if [[ $i -eq 0 ]]; then
echo -n "$(echo "${h:i:1}" | tr '[:lower:]' '[:upper:]')${h:i+1}"
elif [[ $((i+1)) -eq ${#h} ]]; then
echo -n "${h:0:i}$(echo "${h:i:1}" | tr '[:lower:]' '[:upper:]')"
else
echo -n "${h:0:i}$(echo "${h:i:1}" | tr '[:lower:]' '[:upper:]')${h:i+1}"
fi
else
echo -n "A${h:1}"
fi
else
echo -n "$h"
fi
}
get_first_letter() {
#1: str
echo "$1" | grep -o -E '[a-z]' | head -1
}
get_str_index() {
#1: str
#2: match
local s
s="${1%%$2*}"
[[ "$s" == "$1" ]] && echo -1 || echo "${#s}"
}
set_args() {
expr "$*" : ".*--help" > /dev/null && usage
_UPPERCASE=false
while getopts ":hscxp" opt; do
case $opt in
s)
_SHOW_IT=true
;;
c)
_UPPERCASE=true
;;
x)
_HEX_OUT=true
;;
p)
_PIN_OUT=true
_HEX_OUT=false
_UPPERCASE=false
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
shift $((OPTIND-1))
_LENGTH="$*"
if [[ -z "${_LENGTH:-}" ]]; then
if [[ -z "${_PIN_OUT:-}" ]]; then
_LENGTH=29
else
_LENGTH=4
fi
fi
}
get_platform() {
local platform
platform="$(uname -s)"
if [[ "$platform" == *"inux"* ]]; then
uname -a | awk '{print $NF}'
elif [[ "$platform" == *"arwin"* ]]; then
echo "macos"
else
echo "OS not support!" && exit 1
fi
}
get_string() {
local str
echo -n "Enter a string: " >&2
read -rs str
echo "$str"
}
main() {
set_args "$@"
set_var
local p s c
c="${_LENGTH:-}"
s=$(get_string)
echo ""
if [[ "${_SHOW_IT:-}" ]]; then
hash "$s$_SALT" "$c" $_UPPERCASE
else
p=$(get_platform)
if [[ "$p" == 'GNU/Linux' ]]; then
hash "$s$_SALT" "$c" $_UPPERCASE | xclip -selection c
fi
if [[ "$p" == 'Android' ]]; then
termux-clipboard-set "$(hash "$s$_SALT" "$c" $_UPPERCASE)"
fi
if [[ "$p" == 'macos' ]]; then
hash "$s$_SALT" "$c" $_UPPERCASE | pbcopy
fi
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi