-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
36 lines (32 loc) · 959 Bytes
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package hash
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
)
// Serialize serializes passed struct to bytes using GOB
func Serialize(object interface{}) ([]byte, error) {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
if err := enc.Encode(object); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// ComputeHash computes hash for passed struct using default Serialize function
func ComputeHash(object interface{}) (string, error) {
return ComputeHashWithSerialize(object, Serialize)
}
// ComputeHashWithSerialize computes hash based on passed struct
// before computing hash, serializes object using provided function
func ComputeHashWithSerialize(object interface{}, serialize func(object interface{}) ([]byte, error)) (string, error) {
bytes, err := serialize(object)
if err != nil {
return "", err
}
h := sha256.New()
h.Write(bytes)
hashed := h.Sum(nil)
return hex.EncodeToString(hashed), nil
}