package/linux-firmware: split bcm43xx / bcm43xxx
[buildroot-gz.git] / support / download / check-hash
blobf1e0c1b6807858478dc0c3a5207c5b039908d34e
1 #!/bin/bash
2 set -e
4 # Helper to check a file matches its known hash
5 # Call it with:
6 # $1: the full path to the file to check
7 # $2: the path of the file containing all the the expected hashes
9 h_file="${1}"
10 file="${2}"
12 # Does the hash-file exist?
13 if [ ! -f "${h_file}" ]; then
14 exit 0
17 # Check one hash for a file
18 # $1: known hash
19 # $2: file (full path)
20 check_one_hash() {
21 _h="${1}"
22 _known="${2}"
23 _file="${3}"
25 # Note: md5 is supported, but undocumented on purpose.
26 # Note: sha3 is not supported, since there is currently no implementation
27 # (the NIST has yet to publish the parameters).
28 case "${_h}" in
29 md5|sha1) ;;
30 sha224|sha256|sha384|sha512) ;;
31 *) # Unknown hash, exit with error
32 printf "ERROR: unknown hash '%s' for '%s'\n" \
33 "${_h}" "${_file##*/}" >&2
34 exit 1
36 esac
38 # Do the hashes match?
39 _hash=$( ${_h}sum "${_file}" |cut -d ' ' -f 1 )
40 if [ "${_hash}" = "${_known}" ]; then
41 printf "%s: OK (%s: %s)\n" "${_file##*/}" "${_h}" "${_hash}"
42 return 0
45 printf "ERROR: %s has wrong %s hash:\n" "${_file##*/}" "${_h}" >&2
46 printf "ERROR: expected: %s\n" "${_known}" >&2
47 printf "ERROR: got : %s\n" "${_hash}" >&2
48 printf "ERROR: Incomplete download, or man-in-the-middle (MITM) attack\n" >&2
50 exit 1
53 # Do we know one or more hashes for that file?
54 nb_checks=0
55 while read t h f; do
56 case "${t}" in
57 ''|'#'*)
58 # Skip comments and empty lines
59 continue
62 if [ "${f}" = "${file##*/}" ]; then
63 check_one_hash "${t}" "${h}" "${file}"
64 : $((nb_checks++))
67 esac
68 done <"${h_file}"
70 if [ ${nb_checks} -eq 0 ]; then
71 if [ -n "${BR2_ENFORCE_CHECK_HASH}" ]; then
72 printf "ERROR: No hash found for %s\n" "${file}" >&2
73 exit 1
74 else
75 printf "WARNING: No hash found for %s\n" "${file}" >&2