Go: hash: allow Hash to implement encoding.BinaryMarshaler, encoding.BinaryUnmarshaler

Created on 4 Jun 2017  ·  4Comments  ·  Source: golang/go

There are some use cases that aren't met by current hashing functions

  • Serialize state of a partial byte stream processing for later work; recalculating the hash from the beginning is inefficient and, in our case, infeasible due to the size of the data and legacy compatibility requirements that inhibit use of, say, a Merkle tree.
  • Sharing calculation of common prefix hashes; this only requires in-memory copying rather than serialization.
  • Conditional append based on hash matches, which is a corollary of the previous.

Previous thread on golang-dev.

In my specific case, I'm looking for MD5 [de]serialization.

/cc @jlhawn

FeatureRequest FrozenDueToAge Proposal Proposal-Accepted

Most helpful comment

Seems like we could make the concrete types implement https://golang.org/pkg/encoding/#BinaryMarshaler and https://golang.org/pkg/encoding/#BinaryUnmarshaler at least.

We can't change the signature of e.g. https://golang.org/pkg/crypto/md5/#New but we could document in an Example that you can type-assert that the returned hash.Hash is also an encoding.BinaryUnmarshaler etc.

All 4 comments

Seems like we could make the concrete types implement https://golang.org/pkg/encoding/#BinaryMarshaler and https://golang.org/pkg/encoding/#BinaryUnmarshaler at least.

We can't change the signature of e.g. https://golang.org/pkg/crypto/md5/#New but we could document in an Example that you can type-assert that the returned hash.Hash is also an encoding.BinaryUnmarshaler etc.

Any objections to hashes implementing BinaryMarshaler and BinaryUnmarshaler? Will leave this open for a week.

Proposal accepted. CLs welcome for the Go 1.10 cycle for

  • Updating hash.Hash docs to document the convention.
  • Updating hash/* and crypto/* hashes to implement it.

Change https://golang.org/cl/66710 mentions this issue: crypto, hash: implement BinaryMarshaler, BinaryUnmarshaler in hash implementations

Was this page helpful?
0 / 5 - 0 ratings