use compress with base64|gzip

This commit is contained in:
Patrick Cao Huu Thien 2024-10-18 00:07:09 +02:00
parent b30bf206ba
commit 10a92d2456
Signed by untrusted user who does not match committer: pcao
GPG Key ID: B57DBE40C72FBCF4
2 changed files with 9 additions and 9 deletions

3
README
View File

@ -1,13 +1,12 @@
*shasumscript* is a tool to generate a self-checksum-script. *shasumscript* is a tool to generate a self-checksum-script.
Usage Usage
shasumscript script.sh shasumscript script.sh
License License
GNU-GPL-3 GNU-GPL-3
From an idea of LSC https://github.com/dealfonso/lsc From an idea of LSC https://github.com/dealfonso/lsc

View File

@ -13,6 +13,8 @@ err() { error "$*"; exit 1; }
usage() { echo "Usage: $(basename "$0") [-V | -h] <file>";echo " -V: Show version"; echo " -h: Show this help"; } usage() { echo "Usage: $(basename "$0") [-V | -h] <file>";echo " -V: Show version"; echo " -h: Show this help"; }
err_usage() { error "$*"; usage; exit 1; } 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"; } 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; }
VERSION="0.0.1" VERSION="0.0.1"
if test "$1" = '-V'; then echo "shasumscript v$VERSION"; exit; fi if test "$1" = '-V'; then echo "shasumscript v$VERSION"; exit; fi
@ -25,6 +27,9 @@ test -e "$file" || err "File not found: $file"
sha="$(shasum -a 256 "$file" | cut -d' ' -f1)" || err "Failed to calculate SHA256 checksum" sha="$(shasum -a 256 "$file" | cut -d' ' -f1)" || err "Failed to calculate SHA256 checksum"
step "Checksum" step "Checksum"
bin=$(cat "$file" | compress)
step "Compress"
newfile="${file}_sha256" newfile="${file}_sha256"
cat <<EOT > "$newfile" cat <<EOT > "$newfile"
@ -32,8 +37,7 @@ cat <<EOT > "$newfile"
# script generated by shasumscript at $(date) # script generated by shasumscript at $(date)
# #
# This script self-checkssum it's content and exit on error # This script self-checkssum it's content and exit on error
# The real script can be found after the separator: # The real script can be found after line 22.
# $sha
# #
# License: GNU GPL3 # License: GNU GPL3
# Author: Patrick Cao Huu Thien # Author: Patrick Cao Huu Thien
@ -41,22 +45,19 @@ cat <<EOT > "$newfile"
tmpexe="\$(mktemp)" tmpexe="\$(mktemp)"
trap 'rm -f "\$tmpexe"' EXIT trap 'rm -f "\$tmpexe"' EXIT
cat "\$0" | sed -n '/^${sha}/,\$p' | tail -n +2 > "\$tmpexe" cat "\$0" | sed '1,21d' | base64 -d 2>/dev/null| gunzip 2>/dev/null > "\$tmpexe"
test "\$(shasum -a 256 "\$tmpexe" | cut -d' ' -f1)" = "$sha" || { test "\$(shasum -a 256 "\$tmpexe" | cut -d' ' -f1)" = "$sha" || {
echo "Checksum mismatch!" >&2 echo "Checksum mismatch!" >&2
exit 1 exit 1
} }
printf "Checksum OK"; sleep 1; printf "\r \r"
sh "\$tmpexe" "\$@" sh "\$tmpexe" "\$@"
exit exit
$sha
EOT EOT
step "pre-script" step "pre-script"
cat "$file" >> "$newfile" echo "$bin" >> "$newfile"
step "post-script" step "post-script"
chmod +x "$newfile" chmod +x "$newfile"