sha256.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #! /usr/bin/env sh
  2. # Copyright (C) 2019 ng0 <ng0@n0.is>
  3. # Copyright (C) 2019 Jeremiah Orians
  4. #
  5. # This file is part of mescc-tools
  6. #
  7. # mescc-tools is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or (at
  10. # your option) any later version.
  11. #
  12. # mescc-tools is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  19. set -ex
  20. # It's bad to rely on the uname here, but it's a start.
  21. # What this does is to consider the major implementations of
  22. # sha256sum tools and their differences and call them
  23. # accordingly.
  24. sha256_check()
  25. {
  26. # Address locale behaviors
  27. export LC_ALL=C
  28. export LANG=C
  29. export LANGUAGE=C
  30. # Sort out what tool they have that can do SHA256SUM calculations and then use it
  31. if [ -e "$(which sha256sum)" ]; then
  32. sha256sum -c "$1"
  33. elif [ "$(./bin/get_machine ${GET_MACHINE_OS_FLAGS} --os)" = "FreeBSD" ]; then
  34. awk '
  35. BEGIN { status = 0 }
  36. {
  37. rc=system(">/dev/null sha256 -q -c "$1" "$2);
  38. if (rc == 0) print($2": OK")
  39. else {
  40. print($2": NOT OK");
  41. status=rc
  42. }
  43. }
  44. END { exit status}
  45. ' "$1"
  46. elif [ -e "$(which sum)" ]; then
  47. sum -a SHA256 -n -c "$1"
  48. elif [ -e "$(which sha256)" ]; then
  49. sha256 -r -c "$1"
  50. else
  51. echo "Unsupported sha256 tool, please send a patch to support it"
  52. exit 77
  53. fi
  54. }