2021-04-28 16:10:44 -04:00
|
|
|
package pb
|
|
|
|
|
|
|
|
import (
|
2022-03-23 09:16:11 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
|
2023-02-06 18:16:20 -04:00
|
|
|
proto "google.golang.org/protobuf/proto"
|
2021-04-28 16:10:44 -04:00
|
|
|
)
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// Hash calculates the hash of a waku message
|
2022-11-03 09:53:33 -04:00
|
|
|
func (msg *WakuMessage) Hash() ([]byte, int, error) {
|
2021-04-28 16:10:44 -04:00
|
|
|
out, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
2022-11-03 09:53:33 -04:00
|
|
|
return nil, 0, err
|
2021-04-28 16:10:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 09:53:33 -04:00
|
|
|
return Hash(out), len(out), nil
|
2021-04-28 16:10:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:16:11 -04:00
|
|
|
// Hash calculates a hash from a byte slice using sha2-256 for the hashing algorithm
|
2021-04-28 16:10:44 -04:00
|
|
|
func Hash(data []byte) []byte {
|
2022-03-23 09:16:11 -04:00
|
|
|
hash := sha256.Sum256(data)
|
|
|
|
return hash[:]
|
2021-04-28 16:10:44 -04:00
|
|
|
}
|