github.com/scottcagno/storage@v1.8.0/pkg/hash/murmur3/murmur64.go (about)

     1  /*
     2   * // Copyright (c) 2021. Scott Cagno. All rights reserved.
     3   * // The license can be found in the root of this project; see LICENSE.
     4   */
     5  
     6  package murmur3
     7  
     8  import (
     9  	"hash"
    10  )
    11  
    12  // Make sure interfaces are correctly implemented.
    13  var (
    14  	_ hash.Hash   = new(digest64)
    15  	_ hash.Hash64 = new(digest64)
    16  	_ bmixer      = new(digest64)
    17  )
    18  
    19  // digest64 is half a digest128.
    20  type digest64 digest128
    21  
    22  func New64() hash.Hash64 {
    23  	d := (*digest64)(New128().(*digest128))
    24  	return d
    25  }
    26  
    27  func (d *digest64) Sum(b []byte) []byte {
    28  	h1 := d.h1
    29  	return append(b,
    30  		byte(h1>>56), byte(h1>>48), byte(h1>>40), byte(h1>>32),
    31  		byte(h1>>24), byte(h1>>16), byte(h1>>8), byte(h1))
    32  }
    33  
    34  func (d *digest64) Sum64() uint64 {
    35  	h1, _ := (*digest128)(d).Sum128()
    36  	return h1
    37  }
    38  
    39  // Sum64 returns the MurmurHash3 sum of data. It is equivalent to the
    40  // following sequence (without the extra burden and the extra allocation):
    41  //     hasher := New64()
    42  //     hasher.WriteType(data)
    43  //     return hasher.Sum64()
    44  func Sum64(data []byte) uint64 {
    45  	d := &digest128{h1: 0, h2: 0}
    46  	d.tail = d.bmix(data)
    47  	d.clen = len(data)
    48  	h1, _ := d.Sum128()
    49  	return h1
    50  }