4 # Helper to check a file matches its known hash
6 # $1: the path of the file containing all the expected hashes
7 # $2: the full path to the temporary file that was downloaded, and
8 # that is to be checked
9 # $3: the final basename of the file, to which it will be ultimately
10 # saved as, to be able to match it to the corresponding hashes
14 # 0: the hash file exists and the file to check matches all its hashes,
15 # or the hash file does not exist
16 # 1: unknown command-line option
17 # 2: the hash file exists and the file to check does not match at least
19 # 3: the hash file exists and there was no hash to check the file against
20 # 4: the hash file exists and at least one hash type is unknown
22 while getopts :q OPT
; do
34 # Bail early if no hash to check
35 if [ -z "${h_file}" ]; then
38 # Does the hash-file exist?
39 if [ ! -f "${h_file}" ]; then
40 printf "WARNING: no hash file for %s\n" "${base}" >&2
44 # Check one hash for a file
46 # $2: file (full path)
52 # Note: md5 is supported, but undocumented on purpose.
53 # Note: sha3 is not supported, since there is currently no implementation
54 # (the NIST has yet to publish the parameters).
55 # Note: 'none' means there is explicitly no hash for that file.
61 sha224|sha256|sha384|sha512
) ;;
62 *) # Unknown hash, exit with error
63 printf "ERROR: unknown hash '%s' for '%s'\n" \
69 # Do the hashes match?
70 _hash
=$
( ${_h}sum "${_file}" |cut
-d ' ' -f 1 )
71 if [ "${_hash}" = "${_known}" ]; then
72 printf "%s: OK (%s: %s)\n" "${base}" "${_h}" "${_hash}"
76 printf "ERROR: %s has wrong %s hash:\n" "${base}" "${_h}" >&2
77 printf "ERROR: expected: %s\n" "${_known}" >&2
78 printf "ERROR: got : %s\n" "${_hash}" >&2
79 printf "ERROR: Incomplete download, or man-in-the-middle (MITM) attack\n" >&2
84 # Do we know one or more hashes for that file?
89 # Skip comments and empty lines
93 if [ "${f}" = "${base}" ]; then
94 check_one_hash
"${t}" "${h}" "${file}"
101 if [ ${nb_checks} -eq 0 ]; then
102 case " ${BR_NO_CHECK_HASH_FOR} " in
104 # File explicitly has no hash
108 printf "ERROR: No hash found for %s\n" "${base}" >&2