-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
checksum.go
53 lines (45 loc) · 1.17 KB
/
checksum.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
45
46
47
48
49
50
51
52
53
// Package checksum provides functions to generate a hash sum
// from any given object.
package checksum
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"hash"
)
// Sum returns a hash string from the given object v using
// the given hash function hasher.
func Sum(v interface{}, hasher hash.Hash) (hash string, err error) {
data, err := json.Marshal(v)
if err != nil {
return
}
hash = hex.EncodeToString(hasher.Sum(data))
return
}
// SumSha1 is shorthand for Sum using the sha1 hash function.
func SumSha1(v interface{}) (string, error) {
return Sum(v, sha1.New())
}
// SumSha256 is shorthand for Sum using the sha256 hash function.
func SumSha256(v interface{}) (string, error) {
return Sum(v, sha256.New())
}
// SumMd5 is shorthand for Sum using the md5 hash function.
func SumMd5(v interface{}) (string, error) {
return Sum(v, md5.New())
}
// Must can be used to execute a hash function and panics
// if the hash generation returned an error.
//
// Example:
// myObject := &MyObject{}
// hash := checksum.Must(checksum.SumSha1(myObject))
func Must(hash string, err error) string {
if err != nil {
panic(err)
}
return hash
}