oa8sign 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env sh
  2. set -e -u
  3. if ! env -i command -p rm -f "$(env -i command -p mktemp)"; then
  4. printf '%s\n' "Error: rm could not be located."
  5. exit 1
  6. fi
  7. cleanup() { env -i command -p rm -f "${tmpfile:-}" || true; }
  8. filename=${1:-}
  9. privatekey=${2:-}
  10. if ! env command openssl version >/dev/null 2>&1; then
  11. printf '%s\n' "Error: openssl could not be located."
  12. exit 1
  13. fi
  14. if ! env command ascii85 -h >/dev/null 2>&1; then
  15. printf '%s\n' "Error: ascii85 could not be located."
  16. printf '%s\n' 'Suggestion: "gem install Ascii85".'
  17. exit 1
  18. fi
  19. if [ "${#}" -lt 2 ]; then
  20. printf '%s\n' "*** oa8sign (v1.0.0)"
  21. printf '%s\n' "Usage: oa8sign <file> <private.oa8>"
  22. exit 1
  23. fi
  24. if [ ! -f "${filename:?Error: filename undefined.}" ]; then
  25. printf '%s\n' "Error: file not found."
  26. exit 1
  27. fi
  28. if [ -f "${filename:?Error: filename undefined.}.oa8" ]; then
  29. printf '%s\n' \"Error: "${filename:?Error: filename undefined.}".oa8 exists.\"
  30. exit 1
  31. fi
  32. if [ ! -f "${privatekey:?Error: privatekey undefined.}" ]; then
  33. printf '%s\n' "Error: privatekey not found."
  34. exit 1
  35. fi
  36. tmpfile=$(env command -p mktemp) ||
  37. {
  38. printf '%s\n' "Error: mktemp failure."
  39. exit 1
  40. }
  41. env command openssl dgst -sha3-512 \
  42. -sign "${privatekey:?Error: privatekey undefined.}" \
  43. -out "${tmpfile:?Error: tmpfile undefined.}" \
  44. "${filename:?Error: filename undefined.}" ||
  45. {
  46. printf '%s\n' "Error: openssl failure."
  47. cleanup
  48. exit 1
  49. }
  50. printf '****** BEGIN OA8 SIGNATURE ******\n%s\n****** END OA8 SIGNATURE ******\n' \
  51. "$(env command ascii85 -w 61 \
  52. <"${tmpfile:?Error: filename undefined.}")" \
  53. >"${filename:?Error: filename undefined.}.oa8" ||
  54. {
  55. printf '%s\n' "Error: Writing signature failed."
  56. cleanup
  57. exit 1
  58. }
  59. cleanup || true