forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksum
More file actions
executable file
·127 lines (107 loc) · 2.38 KB
/
Copy pathchecksum
File metadata and controls
executable file
·127 lines (107 loc) · 2.38 KB
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
#!/usr/bin/env bash
#
# Verify the checksum of a file.
#
# Usage: checksum <filepath> <hash>
#
# Arguments:
#
# filepath File to verify.
# hash Expected checksum.
#
# VARIABLES #
# Checksum type:
checksum_type='sha256'
# Program used to compute a checksum:
checksum_program=
# File to verify:
file=
# Command to compute a checksum:
cmd=
# Expected checksum:
expected_checksum=
# Actual checksum:
curr_checksum=
# FUNCTIONS #
# Prints usage information.
usage() {
echo '' >&2
echo 'Usage: checksum <filepath> <hash>' >&2
echo '' >&2
}
# Prints a success message.
print_success() {
echo 'Success.' >&2
}
# Prints a checksum.
#
# $1 - checksum
print_checksum() {
local num_lines
local width
local lines
local len
local pos
width=64 # characters
len="${#1}"
if [[ "${len}" -gt "${width}" ]]; then
num_lines=$(("${len}" / "${width}")) # floored
lines=$(seq 0 1 "${num_lines}") # total_lines = num_lines + 1
for i in "${lines}"; do
pos=$((i * "${width}"))
echo " ${str:${pos}:${width}}"
done
else
echo " $1" >&2
fi
}
# Prints a checksum error.
#
# $1 - file
# $2 - checksum type
# $3 - checksum program
# $4 - expected checksum
# $5 - actual checksum
print_checksum_error() {
echo '' >&2
echo "ERROR: $2 checksum failure for $1. Expected:" >&2
echo '' >&2
print_checksum "$4"
echo '' >&2
echo "But \`$3\` results in:" >&2
echo '' >&2
print_checksum "$5"
echo '' >&2
echo "This may be due to bad downloads or network proxies. Check your network proxy/" >&2
echo "firewall settings and try downloading and verifying again." >&2
echo '' >&2
}
# MAIN #
# Handle arguments...
if [[ "$#" -eq 0 ]]; then
usage
exit 0
elif [[ "$#" -eq 2 ]]; then
file="$1"
expected_checksum="$2"
else
echo 'ERROR: unrecognized arguments. Must provide a file to verify and an expected checksum.' >&2
exit 1
fi
# Find a program for computing a SHA256 checksum...
if [[ -n "$(which sha256sum)" ]]; then
checksum_program="sha256sum"
cmd="sha256sum ${file} | awk '{ print \$1; }'"
elif [[ -n "$(which shasum)" ]]; then
checksum_program="shasum"
cmd="shasum -a 256 ${file} | awk '{ print \$1; }'"
fi
# Verify checksum:
curr_checksum=$(eval "${cmd}")
if [[ "${curr_checksum}" != "${expected_checksum}" ]]; then
print_checksum_error "${file}" "${checksum_type}" "${checksum_program}" "${expected_checksum}" "${curr_checksum}"
exit 1
else
print_success
exit 0
fi