Files
gitea/tools/hash-id

35 lines
773 B
Bash
Executable File

#!/usr/bin/env sh
# hash-id — 產生 8 碼大寫 SHA-1 hash。
# 用法:
# hash-id <text>
# printf '%s' <text> | hash-id
# 規則:
# 先取 SHA-1 前 8 碼大寫。
# 若首碼為 0-9、A、B、C,改成 H 加上原 SHA-1 前 7 碼。
set -eu
input=''
if [ "$#" -gt 0 ]; then
input=$*
elif [ -t 0 ]; then
input=''
else
input=$(cat)
fi
if command -v sha1sum >/dev/null 2>&1; then
raw=$(printf '%s' "$input" | sha1sum | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
raw=$(printf '%s' "$input" | shasum -a 1 | awk '{print $1}')
else
echo 'no SHA-1 helper found' >&2
exit 1
fi
hash=$(printf '%s' "$raw" | cut -c1-8 | tr a-f A-F)
case "$hash" in
[0-9ABC]*) hash="H$(printf '%s' "$hash" | cut -c1-7)" ;;
esac
printf '%s\n' "$hash"