+function record_hashes() {
+ if [[ "$mode" == d ]] || (( $# < 2 ));then
+ cat
+ else
+ stage=$1
+ hash=$2
+ shift 2
+ tee >(openssl dgst -binary "-$hash" | base64 --wrap=0 |
+ sed "s/^/openssl-dgst $hash /;s/$/\n/" > "$hash_dir/$stage-$hash") |
+ record_hashes "$stage" "$@"
+ fi
+}
+
+record_hashes inner "${hashes[@]}" | go "$first_layer" | record_hashes outer "${hashes[@]}"
+
+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"
+ cat "$hash_dir"/outer-* "$key_aside" "$hash_dir"/inner* > "$keyfile"
+ shred -u "$key_aside"
+ rmdir "$key_aside_dir"
+else
+ # Verify the hashes
+ for hash_result in "$hash_dir"/*;do
+ verify_hash "$hash_result" || die "Hash check $(basename "$hash_result") failed"
+ done
+fi
+
+rm -r "$hash_dir"