github.com/ethersphere/bee/v2@v2.2.0/pkg/swarm/hasher.go (about)

     1  // Copyright 2023 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package swarm
     6  
     7  import (
     8  	"hash"
     9  
    10  	"golang.org/x/crypto/sha3"
    11  )
    12  
    13  // NewHasher returns new Keccak-256 hasher.
    14  func NewHasher() hash.Hash {
    15  	return sha3.NewLegacyKeccak256()
    16  }
    17  
    18  type PrefixHasher struct {
    19  	hash.Hash
    20  	prefix []byte
    21  }
    22  
    23  // NewPrefixHasher returns new hasher which is Keccak-256 hasher
    24  // with prefix value added as initial data.
    25  func NewPrefixHasher(prefix []byte) hash.Hash {
    26  	h := &PrefixHasher{
    27  		Hash:   NewHasher(),
    28  		prefix: prefix,
    29  	}
    30  	h.Reset()
    31  
    32  	return h
    33  }
    34  
    35  func (h *PrefixHasher) Reset() {
    36  	h.Hash.Reset()
    37  	_, _ = h.Write(h.prefix)
    38  }