github.com/MetalBlockchain/metalgo@v1.11.9/utils/bloom/hasher.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package bloom
     5  
     6  import (
     7  	"crypto/sha256"
     8  	"encoding/binary"
     9  )
    10  
    11  func Add(f *Filter, key, salt []byte) {
    12  	f.Add(Hash(key, salt))
    13  }
    14  
    15  func Contains(c Checker, key, salt []byte) bool {
    16  	return c.Contains(Hash(key, salt))
    17  }
    18  
    19  type Checker interface {
    20  	Contains(hash uint64) bool
    21  }
    22  
    23  func Hash(key, salt []byte) uint64 {
    24  	hash := sha256.New()
    25  	// sha256.Write never returns errors
    26  	_, _ = hash.Write(key)
    27  	_, _ = hash.Write(salt)
    28  
    29  	output := make([]byte, 0, sha256.Size)
    30  	return binary.BigEndian.Uint64(hash.Sum(output))
    31  }