-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash.go
44 lines (37 loc) · 1.14 KB
/
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
37
38
39
40
41
42
43
44
package dataset
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/mr-tron/base58/base58"
"github.com/multiformats/go-multihash"
)
// JSONHash calculates the hash of a json.Marshaler
// It's important to note that this is *NOT* the same as an IPFS hash,
// These hash functions should be used for other things like
// checksumming, in-memory content-addressing, etc.
func JSONHash(m json.Marshaler) (hash string, err error) {
// marshal to cannoncical JSON representation
data, err := m.MarshalJSON()
if err != nil {
return
}
return HashBytes(data)
}
// HashBytes generates the base-58 encoded SHA-256 hash of a byte slice
// It's important to note that this is *NOT* the same as an IPFS hash,
// These hash functions should be used for other things like
// checksumming, in-memory content-addressing, etc.
func HashBytes(data []byte) (hash string, err error) {
h := sha256.New()
if _, err = h.Write(data); err != nil {
return
}
mhBuf, err := multihash.Encode(h.Sum(nil), multihash.SHA2_256)
if err != nil {
err = fmt.Errorf("error allocating multihash buffer: %s", err.Error())
return
}
hash = base58.Encode(mhBuf)
return
}