Skip to content

hash: add Combine functions #12297

@dsnet

Description

@dsnet

I propose that the standard libraries hash/crc32, hash/crc64, and hash/adler32 add a Combine function that does the following:

Let AB be the string concatenation of strings A and B. Then it is possible to define a efficient function Combine that computes Hash(AB) given only Hash(A), Hash(B), Len(B).

Hash(AB) == Combine(Hash(A), Hash(B), Len(B))

Thus, usage of Combine in hash/crc32 may look like:

var s1, s2 []byte // Let these be two large byte strings
crc1 := crc32.ChecksumIEEE(s1)
crc2 := crc32.ChecksumIEEE(s2)
len2 := int64(len(s2))

// crc3 is equivalent to crc32.ChecksumIEEE(append(s1, s2...))
crc3 := crc32.Combine(crc32.IEEE, crc1, crc2, len2) 

The zlib library provides these functions and they are extremely useful for merging large data segments under a single hash or for computing the hash of a large dataset in parallel.

Advantages

  • Add functionality that is obviously related to each of the hash libraries and has clear usefulness and prevents users from needing to implement these non-trivial functions themselves.

Disadvantages

  • The hash libraries will be slightly asymmetrical since hash/fnv won't have a Combine function. I haven't spent too much time on it, but it may be possible to define a Combine function for fnv as well.

I'd be interested in adding these if other people find them useful as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions