Compare commits
No commits in common. "master" and "main" have entirely different histories.
13
README
13
README
|
@ -1,13 +0,0 @@
|
||||||
*shasumscript* is a tool to generate a self-checksum-script.
|
|
||||||
|
|
||||||
Usage
|
|
||||||
|
|
||||||
shasumscript script.sh
|
|
||||||
|
|
||||||
License
|
|
||||||
|
|
||||||
GNU-GPL-3
|
|
||||||
|
|
||||||
From an idea of LSC https://github.com/dealfonso/lsc
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
# shasumscript
|
||||||
|
|
||||||
|
A tool to generate a self-checksum-script.
|
119
shasumscript
119
shasumscript
|
@ -1,119 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
# Script to add a SHA256 checksum to a file
|
|
||||||
# Usage: shasumscript <file>
|
|
||||||
# Output: <file>_sha256
|
|
||||||
# Requires: shasum
|
|
||||||
# License: GNU GPL3
|
|
||||||
# Author: Patrick Cao Huu Thien
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
error() { printf "Error: \e[31m%s\e[0m\n" "$*" >&2; }
|
|
||||||
err() { error "$*"; exit 1; }
|
|
||||||
usage() { echo "Usage: $(basename "$0") [-V | -h] <file>";echo " -V: Show version"; echo " -h: Show this help"; }
|
|
||||||
err_usage() { error "$*"; usage; exit 1; }
|
|
||||||
step () { err="$?"; c=32; t=OK; test "$err" = 0 || { c=31; t=FAILED; }; printf "* %s \e[%dm%s\e[0m\n" "$*" "$c" "$t"; }
|
|
||||||
compress() { cat | gzip -9 | base64 -w0; }
|
|
||||||
|
|
||||||
|
|
||||||
VERSION="0.0.1"
|
|
||||||
if test "$1" = '-V'; then echo "shasumscript v$VERSION"; exit; fi
|
|
||||||
if test "$1" = '-h'; then usage; exit; fi
|
|
||||||
|
|
||||||
file="$1"
|
|
||||||
test -n "$file" || err_usage "Missing file argument"
|
|
||||||
test -e "$file" || err "File not found: $file"
|
|
||||||
|
|
||||||
sha="$(shasum -a 256 "$file" | cut -d' ' -f1)" || err "Failed to calculate SHA256 checksum"
|
|
||||||
step "Checksum"
|
|
||||||
|
|
||||||
bin=$(compress < "$file")
|
|
||||||
step "Compress"
|
|
||||||
|
|
||||||
defkey=$(gpgconf --list-options gpg | awk -F: '$1 == "default-key" {print $10}' | tr -dc 'A-Z0-9')
|
|
||||||
step "GPG default key"
|
|
||||||
if test -n "$defkey"
|
|
||||||
then
|
|
||||||
pub=$(gpg --export --output - "$defkey" | compress)
|
|
||||||
step "Public key"
|
|
||||||
sig=$(gpg --detach-sig --output - "$file" | compress)
|
|
||||||
step "Signature"
|
|
||||||
fi
|
|
||||||
|
|
||||||
newfile="${file}_sha256"
|
|
||||||
|
|
||||||
cat <<EOT > "$newfile"
|
|
||||||
#!/bin/sh
|
|
||||||
# script generated by shasumscript at $(date)
|
|
||||||
#
|
|
||||||
# This script self-checkssum it's content and exit on error
|
|
||||||
# The real script can be found after line 37.
|
|
||||||
#
|
|
||||||
# License: GNU GPL3
|
|
||||||
# Author: Patrick Cao Huu Thien
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
sig="$sig"
|
|
||||||
pub="$pub"
|
|
||||||
tmpexe="\$(mktemp)"
|
|
||||||
tmpsig="\$(mktemp)"
|
|
||||||
tmppub="\$(mktemp)"
|
|
||||||
trap 'rm -f "\$tmpexe" "\$tmpsig" "\$tmppub"' EXIT
|
|
||||||
|
|
||||||
cat "\$0" | sed '1,36d' | base64 -d 2>/dev/null| gunzip 2>/dev/null > "\$tmpexe"
|
|
||||||
test "\$(sha256sum "\$tmpexe" | cut -d' ' -f1)" = "$sha" || {
|
|
||||||
echo "Checksum mismatch!" >&2
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
txt='Checksum';printf -- "\r- \$txt";sleep 0.1;printf -- "\r/";sleep 0.1;printf -- "\r|";sleep 0.1;printf -- "\r\\\\";sleep 0.1;printf -- "\r \e[32m%s OK\e[0m" "\$txt";sleep 0.3
|
|
||||||
EOT
|
|
||||||
step "pre-script"
|
|
||||||
if test -n "$defkey"
|
|
||||||
then
|
|
||||||
cat <<EOT >> "$newfile"
|
|
||||||
echo "\$sig" | base64 -d 2>/dev/null | gunzip 2>/dev/null > "\$tmpsig"
|
|
||||||
echo "\$pub" | base64 -d 2>/dev/null | gunzip 2>/dev/null > "\$tmppub"
|
|
||||||
gpg --verify -q --keyring "\$tmppub" "\$tmpsig" "\$tmpexe" 2>&1 | grep -q 'Good signature' || {
|
|
||||||
echo "Signature mismatch!" >&2
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
txt='Verification';printf -- "\r- \$txt";sleep 0.1;printf -- "\r/";sleep 0.1;printf -- "\r|";sleep 0.1;printf -- "\r\\\\";sleep 0.1;printf -- "\r \e[32m%s OK\e[0m" "\$txt";sleep 0.3
|
|
||||||
EOT
|
|
||||||
step "verify-script"
|
|
||||||
else
|
|
||||||
cat <<EOT >> "$newfile"
|
|
||||||
|
|
||||||
#
|
|
||||||
# No public key available
|
|
||||||
#
|
|
||||||
# skip GPG verification
|
|
||||||
#
|
|
||||||
|
|
||||||
EOT
|
|
||||||
step "no-verify-script"
|
|
||||||
fi
|
|
||||||
cat <<EOT >> "$newfile"
|
|
||||||
printf "\r \r"
|
|
||||||
|
|
||||||
sh "\$tmpexe" "\$@"
|
|
||||||
|
|
||||||
exit
|
|
||||||
EOT
|
|
||||||
|
|
||||||
echo "$bin" >> "$newfile"
|
|
||||||
step "binary"
|
|
||||||
|
|
||||||
chmod +x "$newfile"
|
|
||||||
step "make it executable"
|
|
||||||
echo
|
|
||||||
cat <<EOT
|
|
||||||
File created: $newfile
|
|
||||||
|
|
||||||
You can distribute it.
|
|
||||||
|
|
||||||
To run it:
|
|
||||||
./$newfile
|
|
||||||
EOT
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue