-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwt-sum
executable file
·128 lines (114 loc) · 2.84 KB
/
wt-sum
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
#!/bin/sh
# Copyright (C) 2018 Francois Gouget
#
# A portable script to compute cryptographic checksums.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
name0=`basename "$0"`
error()
{
echo "$name0:error:" "$@" >&2
}
### Main
opt_type=""
opt_file=""
usage=""
while [ $# -gt 0 ]
do
arg="$1"
shift
case "$arg" in
--sha1|--sha256)
if [ -n "$opt_type" ]
then
error "the checksum type can only be specified once"
opt_usage=2
break
fi
opt_type="$arg"
;;
--help|-h|-\?)
usage="0"
;;
*)
if [ -n "$opt_file" ]
then
error "only one filename can be specified"
opt_usage=2
break
fi
opt_file="$arg"
esac
done
if [ -z "$usage" ]
then
if [ -z "$opt_type" ]
then
error "you must specify the type of checksum to compute"
usage="2"
fi
if [ -z "$opt_file" ]
then
error "you must specify the file to checksum"
usage="2"
fi
fi
if [ "$usage" = "0" ]
then
echo "Usage: $name0 [--help] (--sha1|--sha256) FILENAME"
echo
echo "Prints the cryptographic checksum of the specified file on stdout."
echo
echo "Where:"
echo " --sha1 Computes the SHA1 cryptographic checksum."
echo " --sha256 Computes the SHA256 cryptographic checksum."
echo " FILENAME Is the file to checksum."
echo " --help, -h Shows this help message."
exit 0
elif [ -n "$usage" ]
then
error "try '$name0 --help' for more information"
exit $usage
fi
sha1_wrapper()
{
if which sha1sum >/dev/null 2>&1
then
sha1sum "$@" | cut -d' ' -f1
elif which shasum >/dev/null 2>&1
then
shasum -a 1 "$@" | cut -d' ' -f1
elif which sha1 >/dev/null 2>&1
then
sha1 "$@" | sed -e 's/^.* = //'
fi
}
sha256_wrapper()
{
if which sha256sum >/dev/null 2>&1
then
sha256sum "$@" | cut -d' ' -f1
elif which shasum >/dev/null 2>&1
then
shasum -a 256 "$@" | cut -d' ' -f1
elif which sha256 >/dev/null 2>&1
then
sha256 "$@" | sed -e 's/^.* = //'
fi
}
case "$opt_type" in
--sha1) sha1_wrapper "$opt_file" ;;
--sha256) sha256_wrapper "$opt_file" ;;
esac
exit $?