Skip to content

proposal: hash: add Cloner #69293

Closed as not planned
Closed as not planned
@qmuntal

Description

@qmuntal

Most hashers in the standard library implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. Using those interfaces does make sense in some situations, with the drawback that they won't work with hashers that don't implement them and that it is using encoding methods to clone feels hacky. For example, the Go team at Microsoft maintain some Go bindings for Windows CNG (see repo), and CNG doesn't support serializing the hash internal state.

See https://github.com/search?q=language%3AGo+h.%28encoding.BinaryMarshaler%29&type=code for a none-exhaustive list of projects that use the encoding.BinaryMarshaler interface.

After analyzing the code of the projects in the list, we found that the encoding.BinaryMarshaler interface is mostly used in the following ways:

  • To serialize the hasher state to disk or to a network connection so that it can be restored later.
  • To clone a hasher so that the cloned hasher does not have to recompute the hash over the same data, which might be expensive or even impossible due to the data not being available anymore.

I propose to define an interface that is easier to implement by hash providers:

type Cloner interface {
    Hash
    Clone() (hash.Hash, error)
}

In addition to the above, we would update all hashers in the stdlib that implement hash.Cloner.

Examples of projects cloning a hash:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions