github.com/spaolacci/murmur3@v1.1.0/murmur64.go (about)

     1  package murmur3
     2  
     3  import (
     4  	"hash"
     5  )
     6  
     7  // Make sure interfaces are correctly implemented.
     8  var (
     9  	_ hash.Hash   = new(digest64)
    10  	_ hash.Hash64 = new(digest64)
    11  	_ bmixer      = new(digest64)
    12  )
    13  
    14  // digest64 is half a digest128.
    15  type digest64 digest128
    16  
    17  // New64 returns a 64-bit hasher
    18  func New64() hash.Hash64 { return New64WithSeed(0) }
    19  
    20  // New64WithSeed returns a 64-bit hasher set with explicit seed value
    21  func New64WithSeed(seed uint32) hash.Hash64 {
    22  	d := (*digest64)(New128WithSeed(seed).(*digest128))
    23  	return d
    24  }
    25  
    26  func (d *digest64) Sum(b []byte) []byte {
    27  	h1 := d.Sum64()
    28  	return append(b,
    29  		byte(h1>>56), byte(h1>>48), byte(h1>>40), byte(h1>>32),
    30  		byte(h1>>24), byte(h1>>16), byte(h1>>8), byte(h1))
    31  }
    32  
    33  func (d *digest64) Sum64() uint64 {
    34  	h1, _ := (*digest128)(d).Sum128()
    35  	return h1
    36  }
    37  
    38  // Sum64 returns the MurmurHash3 sum of data. It is equivalent to the
    39  // following sequence (without the extra burden and the extra allocation):
    40  //     hasher := New64()
    41  //     hasher.Write(data)
    42  //     return hasher.Sum64()
    43  func Sum64(data []byte) uint64 { return Sum64WithSeed(data, 0) }
    44  
    45  // Sum64WithSeed returns the MurmurHash3 sum of data. It is equivalent to the
    46  // following sequence (without the extra burden and the extra allocation):
    47  //     hasher := New64WithSeed(seed)
    48  //     hasher.Write(data)
    49  //     return hasher.Sum64()
    50  func Sum64WithSeed(data []byte, seed uint32) uint64 {
    51  	d := &digest128{h1: uint64(seed), h2: uint64(seed)}
    52  	d.seed = seed
    53  	d.tail = d.bmix(data)
    54  	d.clen = len(data)
    55  	h1, _ := d.Sum128()
    56  	return h1
    57  }