sha256.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. if [ -e "$(which sha256sum)" ]; then
  27. LANG=C sha256sum -c "$1"
  28. elif [ "$(get_machine --OS)" = "FreeBSD" ]; then
  29. LANG=C awk '
  30. BEGIN { status = 0 }
  31. {
  32. rc=system(">/dev/null sha256 -q -c "$1" "$2);
  33. if (rc == 0) print($2": OK")
  34. else {
  35. print($2": NOT OK");
  36. status=rc
  37. }
  38. }
  39. END { exit status}
  40. ' "$1"
  41. elif [ -e "$(which sum)" ]; then
  42. LANG=C sum -a SHA256 -n -c "$1"
  43. elif [ -e "$(which sha256)" ]; then
  44. LANG=C sha256 -r -c "$1"
  45. else
  46. echo "Unsupported sha256 tool, please send a patch to support it"
  47. exit 77
  48. fi
  49. }