Skip to content

Instantly share code, notes, and snippets.

@benstiglitz
Created December 17, 2013 16:49
Show Gist options
  • Save benstiglitz/8008156 to your computer and use it in GitHub Desktop.
Save benstiglitz/8008156 to your computer and use it in GitHub Desktop.
Create a sha256 catalog of a tar file passed on stdin.
#!/bin/bash
# tarripper
# takes a tar file on stdin
# outputs a sha256 catalog of the tar file on stout
#
# public domain
set -e
while true; do
IFS=''
read -n 100 filename
read -n 24 # mode, owner, group
read -n 12 length_octal
read -n 20 # last mod, checksum
read -n 1 type
read -n 188 # linked file, ustar, ustar version, owner name, group name, device major, device minor
read -n 155 prefix
if [ ! -z "$prefix" ] ; then prefix="${prefix}/"; fi
read -n 12 # nothing
length=$((0${length_octal}))
blocks=$((length / 512))
wholeblocks=$blocks
remainder=$((length % 512))
if [[ $remainder -gt 0 ]] ; then blocks=$((${blocks} + 1)); fi
if [[ $type == "0" ]] ; then
hash=$((dd ibs=512 count=$wholeblocks 2>/dev/null; dd ibs=$remainder count=1 2>/dev/null) | openssl dgst -sha256)
dd ibs=1 skip=$(($blocks * 512 - $length)) count=0 of=/dev/null 2>/dev/null
echo "$hash $prefix$filename"
else
# skip ahead
dd ibs=512 skip=$blocks count=0 of=/dev/null 2>/dev/null
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment