summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Bobov <anton@bobov.name>2026-04-20 13:37:54 +0500
committerAnton Bobov <anton@bobov.name>2026-07-24 14:21:47 +0500
commita2506ed51e7f8991672547893e771ee5c594e9ca (patch)
tree5ad72decc77828265c65b6bd217f7f750ed58177
parentf8ee0b083ea75630845a8fbe6662de2e97fa09a3 (diff)
add hashit tool for generating and verifying files checksums
-rwxr-xr-xhashit54
1 files changed, 54 insertions, 0 deletions
diff --git a/hashit b/hashit
new file mode 100755
index 0000000..1f08d7e
--- /dev/null
+++ b/hashit
@@ -0,0 +1,54 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+# @describe Generate or verify SHA512 checksums
+# @meta version 1.0.0
+# @meta require-tools sha512sum
+# @meta combine-shorts
+#
+# @flag -s --single-file Use single hash file (default: $HASHIT_SINGLE_FILE_NAME)
+# @flag -c --check Verify checksums
+# @flag -f --force Overwrite existing hash files
+#
+# @arg files+ Files to hash or verify
+#
+# @env HASHIT_SINGLE_FILE_NAME=SHA512SUMS Hash file name for single-file mode
+
+check_file_and_fail() {
+ if [ -f "$1" ] && [ -z "${argc_force:-}" ]; then
+ echo "error: file '$1' exists; use -f to force overwrite" >&2
+ exit 1
+ fi
+}
+
+main() {
+ if [ -n "${argc_single_file:-}" ]; then
+ if [ -n "${argc_check:-}" ]; then
+ sha512sum -c "$HASHIT_SINGLE_FILE_NAME"
+ else
+ check_file_and_fail "$HASHIT_SINGLE_FILE_NAME"
+ sha512sum "${argc_files[@]?}" >"$HASHIT_SINGLE_FILE_NAME"
+ fi
+ else
+ local hash_file
+ if [ -n "${argc_check:-}" ]; then
+ for file in "${argc_files[@]?}"; do
+ if [[ "$file" == *.sha512 ]]; then
+ hash_file="$file"
+ else
+ hash_file="${file}.sha512"
+ fi
+ sha512sum -c "$hash_file"
+ done
+ else
+ for file in "${argc_files[@]?}"; do
+ hash_file="${file}.sha512"
+ check_file_and_fail "$hash_file"
+ echo "$(sha512sum "$file" | cut -d ' ' -f 1) $(basename "$file")" >"$hash_file"
+ done
+ fi
+ fi
+}
+
+eval "$(argc --argc-eval "$0" "$@")"