From a2506ed51e7f8991672547893e771ee5c594e9ca Mon Sep 17 00:00:00 2001 From: Anton Bobov Date: Mon, 20 Apr 2026 13:37:54 +0500 Subject: add hashit tool for generating and verifying files checksums --- hashit | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 hashit (limited to 'hashit') 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" "$@")" -- cgit v1.2.3