multiformats/js-multihash

bug: inconsistent hash between Go and Js

paralin opened this issue · 2 comments

If I hash the following data in Go:

CAESIMsMcHC401e+Q5Uz4N/dlczloHqo/Bq9ZUe6FUhz5YTH

with:

hash, err := mh.Sum(data, mh.SHA2_256, -1)

I get this multihash:

EiAP+fwMvTH7eTZYPQYIDoUNE/rDSHLjIDfACaOcV3OWrw==

But if I do the same in Javascript:

var toBuffer = require('typedarray-to-buffer')
multihashing.digest(toBuffer(data), "sha2-256")

I get the following:

D/n8DL0x+3k2WD0GCA6FDRP6w0hy4yA3wAmjnFdzlq8=

Which is different, by 2 extra bytes at the beginning (marked with -):

      Object {
        "data": Array [
          10,
    -     34,
    -     18,
          32,
          15,
          249,
          252,
          12,

Why does it have 2 extra bytes at the beginning in JS? It's a real issue for deterministic code.

It seems like you have this in the Go implementation:

	start := make([]byte, 2*binary.MaxVarintLen64)
	spot := start
	n := binary.PutUvarint(spot, code)
	spot = start[n:]
	n += binary.PutUvarint(spot, uint64(len(buf)))

Is this the same in Javascript? I assume you've thought of this, but would it not be wise to use Protobuf varints for consistency?

multihashing.digest(toBuffer(data), "sha2-256")

... whoops, didn't read the docs...