Skip to content

Commit 72a3a1b

Browse files
committed
Add script to verify a checksum
1 parent 39cfaec commit 72a3a1b

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

tools/scripts/checksum

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Verify the checksum of a file.
4+
#
5+
# Usage: checksum <filepath> <hash>
6+
#
7+
# Arguments:
8+
#
9+
# filepath File to verify.
10+
# hash Expected checksum.
11+
#
12+
13+
# VARIABLES #
14+
15+
# Checksum type:
16+
checksum_type='sha256'
17+
18+
# Program used to compute a checksum:
19+
checksum_program=
20+
21+
# File to verify:
22+
file=
23+
24+
# Command to compute a checksum:
25+
cmd=
26+
27+
# Expected checksum:
28+
expected_checksum=
29+
30+
# Actual checksum:
31+
curr_checksum=
32+
33+
34+
# FUNCTIONS #
35+
36+
# Prints usage information.
37+
usage() {
38+
echo '' >&2
39+
echo 'Usage: checksum <filepath> <hash>' >&2
40+
echo '' >&2
41+
}
42+
43+
# Prints a success message.
44+
print_success() {
45+
echo 'Success.' >&2
46+
}
47+
48+
# Prints a checksum.
49+
#
50+
# $1 - checksum
51+
print_checksum() {
52+
local num_lines
53+
local width
54+
local lines
55+
local len
56+
local pos
57+
58+
width=64 # characters
59+
len="${#1}"
60+
if [[ "${len}" -gt "${width}" ]]; then
61+
num_lines=$(("${len}" / "${width}")) # floored
62+
lines=$(seq 0 1 "${num_lines}") # total_lines = num_lines + 1
63+
for i in "${lines}"; do
64+
pos=$((i * "${width}"))
65+
echo " ${str:${pos}:${width}}"
66+
done
67+
else
68+
echo " $1" >&2
69+
fi
70+
}
71+
72+
# Prints a checksum error.
73+
#
74+
# $1 - file
75+
# $2 - checksum type
76+
# $3 - checksum program
77+
# $4 - expected checksum
78+
# $5 - actual checksum
79+
print_checksum_error() {
80+
echo '' >&2
81+
echo "ERROR: $2 checksum failure for $1. Expected:" >&2
82+
echo '' >&2
83+
print_checksum "$4"
84+
echo '' >&2
85+
echo "But \`$3\` results in:" >&2
86+
echo '' >&2
87+
print_checksum "$5"
88+
echo '' >&2
89+
echo "This may be due to bad downloads or network proxies. Check your network proxy/" >&2
90+
echo "firewall settings and try downloading and verifying again." >&2
91+
echo '' >&2
92+
}
93+
94+
95+
# MAIN #
96+
97+
# Handle arguments...
98+
if [[ "$#" -eq 0 ]]; then
99+
usage
100+
exit 0
101+
elif [[ "$#" -eq 2 ]]; then
102+
file="$1"
103+
expected_checksum="$2"
104+
else
105+
echo 'ERROR: unrecognized arguments. Must provide a file to verify and an expected checksum.' >&2
106+
exit 1
107+
fi
108+
109+
# Find a program for computing a SHA256 checksum...
110+
if [[ -n "$(which sha256sum)" ]]; then
111+
checksum_program="sha256sum"
112+
cmd="sha256sum ${file} | awk '{ print \$1; }'"
113+
elif [[ -n "$(which shasum)" ]]; then
114+
checksum_program="shasum"
115+
cmd="shasum -a 256 ${file} | awk '{ print \$1; }'"
116+
fi
117+
118+
# Verify checksum:
119+
curr_checksum=$(eval "${cmd}")
120+
121+
if [[ "${curr_checksum}" != "${expected_checksum}" ]]; then
122+
print_checksum_error "${file}" "${checksum_type}" "${checksum_program}" "${expected_checksum}" "${curr_checksum}"
123+
exit 1
124+
else
125+
print_success
126+
exit 0
127+
fi

0 commit comments

Comments
 (0)