Closed Kcchouette closed 5 years ago
Although Multihash looks interesting, it doesn't supports many popular hash functions, e.g. hash functions supported by RHash. I suppose it is not very popular and is a bit inmature in a way it encodes parametric hash functions, like blake, skein and infohash.
RHash aim to support standards and stable popular formats. So it seems we can't accept multihash in it's current state.
As a workaround, I propose the author to write a Python script using LibRHash bindings as a separate project.
im using the multihash format to store multiple hashes of a single file where i dont need the file path
this is no "strict multihash" format im simply using the algorithm names from the BSD format in lowercase
#!/usr/bin/env bash
# hashes.sh
# create a .hashes file for the input file $1
# MIT license
set -e
set -u
input_path="$1"
file_name="(stdin)"
output_path="$input_path".hashes
# some algos are non-hex by default, for example TTH
hashes="$(rhash --bsd --hex --lowercase --all - <"$input_path")"
if ! echo "$hashes" | grep -q "^BTRH "; then
# add btrh = bittorrent v2 merkle root hash
# https://github.com/rhash/RHash/issues/247
# https://github.com/milahu/bittorrent-v2-merkle-root-hash-py
btrh=$(~/src/milahu/bittorrent-v2-merkle-root-hash-py/btrhsum.py "$input_path")
hashes+=$'\n'"BTRH ($file_name) = $btrh"
fi
function gitblobhash() {
(
printf 'blob %i\0' $(stat -c%s "$1")
cat "$1"
) | sha1sum - | cut -c1-40
}
if ! echo "$hashes" | grep -q "^GITBH "; then
# add git blob hash = sha1 of header and file content
# https://github.com/rhash/RHash/issues/203
gitbh=$(gitblobhash "$input_path")
hashes+=$'\n'"GITBH ($file_name) = $gitbh"
fi
# TODO GITBH-SHA256
if ! echo "$hashes" | grep -q "^SIZE "; then
# add file size in bytes
size=$(stat -c%s "$input_path")
hashes+=$'\n'"SIZE ($file_name) = $size"
fi
# convert algorithm names to lowercase
hashes="$(echo "$hashes" | tr '[:upper:]' '[:lower:]')"
# convert to short format of prefixed hashes, aka multihash https://github.com/multiformats/multihash
# some algos are non-hex by default, for example TTH
hashes="$(echo "$hashes" | sed -E 's/^([^ ]+) .* = ([0-9a-z]+)$/\1:\2/')"
# sort by algorithm name
hashes="$(echo "$hashes" | sort)"
echo "$hashes" >"$output_path"
See https://github.com/multiformats/multihash