rhash / RHash

Great utility for computing hash sums
http://rhash.sf.net
BSD Zero Clause License
586 stars 116 forks source link

Support multihash format #79

Closed Kcchouette closed 5 years ago

Kcchouette commented 5 years ago

See https://github.com/multiformats/multihash

rhash commented 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.

milahu commented 1 year ago

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"
example output ``` echo hello >test.txt hashes.sh test.txt cat test.txt.hashes ``` ``` aich:f572d396fae9206628714fb2ce00f72e94f2258f blake2b:f60ce482e5cc1229f39d71313171a8d9f4ca3a87d066bf4b205effb528192a75f14f3271e2c1a90e1de53f275b4d4793eef2f5e31ea90d2ce29d2e481c36435f blake2s:3969b3926654065966b6f8d9a65789b0f76d56e1e2ab67dd94faa770959187ca btih:6bfe1abd27e9ecfb255a2375a23afbc9f7f7833f btrh:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 crc32:363a3020 crc32c:353dd8be ed2k:63481c78ae04c201fa01ea9d2b1db56d edon-r256:6e265a42c9c109b15ffa63fbd38130bcebcddca57c66d724414145bae602424c edon-r512:8ec5eab3fbb359211e9632259f0bf1c6e757b6df9bf5e57f674578b5e1e83595bf585a9e1480e83c3a56d72c82cbf7838ee939857ec946aa54db592acf6ba3f4 gitbh:ce013625030ba8dba906f756967f9e9ca394464a gost12-256:6ca48d4649e990b9d9404a3431dbc0945bbd4d984631a6cd42d3671f15da6b24 gost12-512:3aadc02fc5091293ea02acb1a5aee2bc06cc0bbe9a59e91477d9817f1642f621739f8278996786be59f87e69f66e2eb0409ead43713dbf46f361c6d754e66937 gost94:6a26855e91b61bc1a9edda9e30e97879db081bf81db2fa7eabe469706947bb74 gost94-cryptopro:fd809f0dfc01406ef6a77871216d405b2303f9268f5a539f5e9e9d7659a3972b has-160:7a9dab6b48229151f03d5ea26c9e93c8d3892030 md4:63481c78ae04c201fa01ea9d2b1db56d md5:b1946ac92492d2347c6235b4d2611184 rmd160:0057b0dc5aac7c215a9a458d6c3c85cd21089af8 sha1:f572d396fae9206628714fb2ce00f72e94f2258f sha224:2d6d67d91d0badcdd06cbbba1fe11538a68a37ec9c2e26457ceff12b sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 sha3-224:5093b1ea1fed43f347b4bf8f8e61334e751516506e390b0fa67758d3 sha3-256:b314e28493eae9dab57ac4f0c6d887bddbbeb810e900d818395ace558e96516d sha3-384:459b2844fea6e3a937a8397c0d69c06d9c6c943e155da454c638f5424296e994fd0339ea234367ff014493b51adb9d2e sha3-512:ac766ba623301e0ad63c48cb2fc469d10145f65c9f1f28fe761c78c386ed295a1fda1b05e280354e620757d8a83e05a45f66438dd734278668c1c27ac6f27150 sha384:1d0f284efe3edea4b9ca3bd514fa134b17eae361ccc7a1eefeff801b9bd6604e01f21f6bf249ef030599f0c218f2ba8c sha512:e7c22b994c59d9cf2b48e549b1e24666636045930d3da7c1acb299d1c3b7f931f94aae41edda2c2b207a36e10f8bcb8d45223e54878f5b316e7ce3b6bc019629 size:6 snefru-128:14b38efcc0f950f42c1cdda2884e25f0 snefru-256:8a1bed0a6c8d2c542f256888086c3f20ec5e6d2f56e17e7cfe0a2e64414eadff tiger:b529747e5337b97818c401dd266799a693165013ce146cac tth:18a7937ebb4b27a48d3eabb34e61126e6a499c9ff5746735 whirlpool:63f8341c1720d76087bb66a8f3fc9ea21a279edeae866a3611ba425cbd9ac9a4e39b97066b492fed6037988a72a045344c8675107e6bb569276c2f597ea9cbb7 ```