github.com/mutagen-io/mutagen@v0.18.0-rc1/sspl/pkg/hashing/xxh128/xxh128.go (about)

     1  //go:build mutagensspl
     2  
     3  // Copyright (c) 2023-present Mutagen IO, Inc.
     4  //
     5  // This program is free software: you can redistribute it and/or modify it under
     6  // the terms of the Server Side Public License, version 1, as published by
     7  // MongoDB, Inc.
     8  //
     9  // This program is distributed in the hope that it will be useful, but WITHOUT
    10  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    11  // FOR A PARTICULAR PURPOSE. See the Server Side Public License for more
    12  // details.
    13  //
    14  // You should have received a copy of the Server Side Public License along with
    15  // this program. If not, see
    16  // <http://www.mongodb.com/licensing/server-side-public-license>.
    17  
    18  package xxh128
    19  
    20  import (
    21  	"hash"
    22  
    23  	"github.com/zeebo/xxh3"
    24  )
    25  
    26  // xxh128Hash implements hash.Hash using the XXH128 algorithm.
    27  type xxh128Hash struct {
    28  	// Hasher is the underlying hasher.
    29  	*xxh3.Hasher
    30  }
    31  
    32  // New returns a new XXH128 hash.
    33  func New() hash.Hash {
    34  	return &xxh128Hash{xxh3.New()}
    35  }
    36  
    37  // Sum implements hash.Hash.Sum.
    38  func (h *xxh128Hash) Sum(b []byte) []byte {
    39  	// Compute the sum and associated bytes.
    40  	sum128 := h.Sum128()
    41  	sum128Bytes := sum128.Bytes()
    42  
    43  	// If b is nil, then take the fast way out.
    44  	if b == nil {
    45  		return sum128Bytes[:]
    46  	}
    47  
    48  	// Otherwise append the bytes to b.
    49  	return append(b, sum128Bytes[:]...)
    50  }