diff --git a/waku/persistence/db_key.go b/waku/persistence/db_key.go index 3eddc6d1..754fb6c3 100644 --- a/waku/persistence/db_key.go +++ b/waku/persistence/db_key.go @@ -1,9 +1,10 @@ package persistence import ( - "crypto/sha256" "encoding/binary" "errors" + + "github.com/waku-org/go-waku/waku/v2/utils" ) const ( @@ -34,7 +35,7 @@ func (k *DBKey) Bytes() []byte { // NewDBKey creates a new DBKey with the given values. func NewDBKey(senderTimestamp uint64, receiverTimestamp uint64, pubsubTopic string, digest []byte) *DBKey { - pubSubHash := sha256.Sum256([]byte(pubsubTopic)) + pubSubHash := utils.SHA256([]byte(pubsubTopic)) var k DBKey k.raw = make([]byte, DBKeyLength) diff --git a/waku/v2/protocol/envelope.go b/waku/v2/protocol/envelope.go index 837b560b..469915ed 100644 --- a/waku/v2/protocol/envelope.go +++ b/waku/v2/protocol/envelope.go @@ -1,10 +1,9 @@ package protocol import ( - "crypto/sha256" - wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/store/pb" + "github.com/waku-org/go-waku/waku/v2/utils" ) // Envelope contains information about the pubsub topic of a WakuMessage @@ -22,7 +21,7 @@ type Envelope struct { // as well as generating a hash based on the bytes that compose the message func NewEnvelope(msg *wpb.WakuMessage, receiverTime int64, pubSubTopic string) *Envelope { messageHash, dataLen, _ := msg.Hash() - hash := sha256.Sum256(append([]byte(msg.ContentTopic), msg.Payload...)) + hash := utils.SHA256(append([]byte(msg.ContentTopic), msg.Payload...)) return &Envelope{ msg: msg, size: dataLen, diff --git a/waku/v2/protocol/relay/waku_relay.go b/waku/v2/protocol/relay/waku_relay.go index c3154e7d..ccc4c027 100644 --- a/waku/v2/protocol/relay/waku_relay.go +++ b/waku/v2/protocol/relay/waku_relay.go @@ -2,11 +2,9 @@ package relay import ( "context" - "crypto/sha256" "encoding/hex" "errors" "fmt" - "hash" "sync" "github.com/libp2p/go-libp2p/core/host" @@ -24,6 +22,7 @@ import ( waku_proto "github.com/waku-org/go-waku/waku/v2/protocol" "github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/timesource" + "github.com/waku-org/go-waku/waku/v2/utils" ) const WakuRelayID_v200 = protocol.ID("/vac/waku/relay/2.0.0") @@ -52,22 +51,8 @@ type WakuRelay struct { subscriptionsMutex sync.Mutex } -var sha256Pool = sync.Pool{New: func() interface{} { - return sha256.New() -}} - func msgIdFn(pmsg *pubsub_pb.Message) string { - h, ok := sha256Pool.Get().(hash.Hash) - if !ok { - h = sha256.New() - } - defer sha256Pool.Put(h) - h.Reset() - - var result [32]byte - h.Write(pmsg.Data) - h.Sum(result[:0]) - return string(result[:]) + return string(utils.SHA256(pmsg.Data)) } // NewWakuRelay returns a new instance of a WakuRelay struct diff --git a/waku/v2/utils/hash.go b/waku/v2/utils/hash.go new file mode 100644 index 00000000..4d27bc08 --- /dev/null +++ b/waku/v2/utils/hash.go @@ -0,0 +1,25 @@ +package utils + +import ( + "crypto/sha256" + "hash" + "sync" +) + +var sha256Pool = sync.Pool{New: func() interface{} { + return sha256.New() +}} + +func SHA256(data []byte) []byte { + h, ok := sha256Pool.Get().(hash.Hash) + if !ok { + h = sha256.New() + } + defer sha256Pool.Put(h) + h.Reset() + + var result [32]byte + h.Write(data) + h.Sum(result[:0]) + return result[:] +}