#!/bin/bash
+umask 077
+
+hash_dir=$(mktemp -d)
+
function die() {
echo "$*" >&2
exit 1
if (( layer == 0 || layer > num_layers ));then
cat
else
- operation=$(sed -n "${layer}s/ .*//p" "$keyfile")
+ operation=$(sed -n "${layer}{;s/ .*//;p;}" "$keyfile")
if [[ "$operation" == openssl-enc ]];then
openssl enc $openssl_decrypt "-$(sed -rn "${layer}s/[^ ]+ ([^ ]+) .*/\\1/p" "$keyfile")" \
-pass fd:37 37< <(sed -rn "${layer}s/^[^ ]+ [^ ]+ //p" "$keyfile")
+ elif [[ "$operation" == reverse ]];then
+ reverse
+ elif [[ "$operation" == openssl-dgst ]];then
+ tee >(echo "$(sed -n "${layer}p" "$keyfile") $(openssl dgst -binary "-$(sed -rn "${layer}s/^[^ ]+ ([^ ]+).*/\\1/p" "$keyfile")" | base64 --wrap=0)" > "$hash_dir/$layer")
else
die "Unknown operation"
fi |
}
go "$first_layer"
+
+for hash_result in "$hash_dir"/*;do
+ layer=$(basename "$hash_result")
+ if [[ "$mode" == e ]];then
+ # Add the hashes to keyfile
+ key_aside_dir=$(mktemp -d "$keyfile.XXXXXXXXXX")
+ key_aside="$key_aside_dir/key.orig"
+ mv "$keyfile" "$key_aside"
+ sed "${layer}s,.*,$(< "$hash_result")," "$key_aside" > "$keyfile"
+ shred -u "$key_aside"
+ rmdir "$key_aside_dir"
+ else
+ # Verify the hashes
+ if [[ "$(awk '{ print $3 == $4 ? "hash ok" : "mismatch" }' "$hash_result")" != "hash ok" ]];then
+ die "Hash check $layer failed"
+ fi
+ fi
+done
+
+rm -r "$hash_dir"