-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhmac-sha1
executable file
·53 lines (38 loc) · 956 Bytes
/
hmac-sha1
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
#!/bin/sh
set -eu
. ./hmac-sha1.sh
prog=$0
usage() {
cat << HERE
Usage: $prog [--binary | --base64] [<key-file>]
Example:
$ printf '%s' value | $prog <(printf '%s' secret_key)
b75db159dc00e1e84e251a1ea6176359e7427901
$ printf '%s' value | KEY=secret_key $prog
b75db159dc00e1e84e251a1ea6176359e7427901
$ printf '%s' value | ./hmac-sha1 --binary <(printf '%s' secret_key) | base64
t12xWdwA4ehOJRoephdjWedCeQE=
$ printf '%s' value | ./hmac-sha1 --base64 <(printf '%s' secret_key)
t12xWdwA4ehOJRoephdjWedCeQE=
HERE
}
if [ $# -eq 0 ] && [ "${KEY:-}" = "" ]; then
usage
exit 1
fi
func="hmac_sha1"
case ${1:-} in
-h | --help) usage && exit 0 ;;
--binary) func="hmac_sha1_binary" && shift ;;
--base64) func="hmac_sha1_base64" && shift ;;
-*) echo "Unknown option $1" >&2 && exit 1 ;;
esac
if [ $# -eq 0 ]; then
key="${KEY:?}"
else
key=$(cat "$1" && echo _)
key=${key%_}
fi
msg=$(cat && echo _)
msg=${msg%_}
"$func" "$key" "$msg"