-
Notifications
You must be signed in to change notification settings - Fork 57
/
sum.go
241 lines (200 loc) · 5.85 KB
/
sum.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package multihash
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha512"
"errors"
"fmt"
blake2b "github.com/minio/blake2b-simd"
sha256 "github.com/minio/sha256-simd"
murmur3 "github.com/spaolacci/murmur3"
blake2s "golang.org/x/crypto/blake2s"
sha3 "golang.org/x/crypto/sha3"
)
// ErrSumNotSupported is returned when the Sum function code is not implemented
var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
var ErrLenTooLarge = errors.New("requested length was too large for digest")
// HashFunc is a hash function that hashes data into digest.
//
// The length is the size the digest will be truncated to. While the hash
// function isn't responsible for truncating the digest, it may want to error if
// the length is invalid for the hash function (e.g., truncation would make the
// hash useless).
type HashFunc func(data []byte, length int) (digest []byte, err error)
// funcTable maps multicodec values to hash functions.
var funcTable = make(map[uint64]HashFunc)
// Sum obtains the cryptographic sum of a given buffer. The length parameter
// indicates the length of the resulting digest and passing a negative value
// use default length values for the selected hash function.
func Sum(data []byte, code uint64, length int) (Multihash, error) {
if !ValidCode(code) {
return nil, fmt.Errorf("invalid multihash code %d", code)
}
if length < 0 {
var ok bool
length, ok = DefaultLengths[code]
if !ok {
return nil, fmt.Errorf("no default length for code %d", code)
}
}
hashFunc, ok := funcTable[code]
if !ok {
return nil, ErrSumNotSupported
}
d, err := hashFunc(data, length)
if err != nil {
return nil, err
}
if len(d) < length {
return nil, ErrLenTooLarge
}
if length >= 0 {
d = d[:length]
}
return Encode(d, code)
}
func sumBlake2s32(data []byte, _ int) ([]byte, error) {
d := blake2s.Sum256(data)
return d[:], nil
}
func sumBlake2b(data []byte, size int) ([]byte, error) {
// special case these lengths to avoid allocations.
switch size {
case 32:
hash := blake2b.Sum256(data)
return hash[:], nil
case 64:
hash := blake2b.Sum512(data)
return hash[:], nil
}
// Ok, allocate away.
hasher, err := blake2b.New(&blake2b.Config{Size: uint8(size)})
if err != nil {
return nil, err
}
if _, err := hasher.Write(data); err != nil {
return nil, err
}
return hasher.Sum(nil)[:], nil
}
func sumID(data []byte, length int) ([]byte, error) {
if length >= 0 && length != len(data) {
return nil, fmt.Errorf("the length of the identity hash (%d) must be equal to the length of the data (%d)",
length, len(data))
}
return data, nil
}
func sumSHA1(data []byte, length int) ([]byte, error) {
a := sha1.Sum(data)
return a[0:20], nil
}
func sumSHA256(data []byte, length int) ([]byte, error) {
a := sha256.Sum256(data)
return a[0:32], nil
}
func sumMD5(data []byte, length int) ([]byte, error) {
a := md5.Sum(data)
return a[0:md5.Size], nil
}
func sumDoubleSHA256(data []byte, length int) ([]byte, error) {
val, _ := sumSHA256(data, len(data))
return sumSHA256(val, len(val))
}
func sumSHA512(data []byte, length int) ([]byte, error) {
a := sha512.Sum512(data)
return a[0:64], nil
}
func sumKeccak256(data []byte, length int) ([]byte, error) {
h := sha3.NewLegacyKeccak256()
h.Write(data)
return h.Sum(nil), nil
}
func sumKeccak512(data []byte, length int) ([]byte, error) {
h := sha3.NewLegacyKeccak512()
h.Write(data)
return h.Sum(nil), nil
}
func sumSHA3_512(data []byte, length int) ([]byte, error) {
a := sha3.Sum512(data)
return a[:], nil
}
func sumMURMUR3(data []byte, length int) ([]byte, error) {
number := murmur3.Sum32(data)
bytes := make([]byte, 4)
for i := range bytes {
bytes[i] = byte(number & 0xff)
number >>= 8
}
return bytes, nil
}
func sumSHAKE128(data []byte, length int) ([]byte, error) {
bytes := make([]byte, 32)
sha3.ShakeSum128(bytes, data)
return bytes, nil
}
func sumSHAKE256(data []byte, length int) ([]byte, error) {
bytes := make([]byte, 64)
sha3.ShakeSum256(bytes, data)
return bytes, nil
}
func sumSHA3_384(data []byte, length int) ([]byte, error) {
a := sha3.Sum384(data)
return a[:], nil
}
func sumSHA3_256(data []byte, length int) ([]byte, error) {
a := sha3.Sum256(data)
return a[:], nil
}
func sumSHA3_224(data []byte, length int) ([]byte, error) {
a := sha3.Sum224(data)
return a[:], nil
}
func registerStdlibHashFuncs() {
RegisterHashFunc(IDENTITY, sumID)
RegisterHashFunc(SHA1, sumSHA1)
RegisterHashFunc(SHA2_512, sumSHA512)
RegisterHashFunc(MD5, sumMD5)
}
func registerNonStdlibHashFuncs() {
RegisterHashFunc(SHA2_256, sumSHA256)
RegisterHashFunc(DBL_SHA2_256, sumDoubleSHA256)
RegisterHashFunc(KECCAK_256, sumKeccak256)
RegisterHashFunc(KECCAK_512, sumKeccak512)
RegisterHashFunc(SHA3_224, sumSHA3_224)
RegisterHashFunc(SHA3_256, sumSHA3_256)
RegisterHashFunc(SHA3_384, sumSHA3_384)
RegisterHashFunc(SHA3_512, sumSHA3_512)
RegisterHashFunc(MURMUR3_128, sumMURMUR3)
RegisterHashFunc(SHAKE_128, sumSHAKE128)
RegisterHashFunc(SHAKE_256, sumSHAKE256)
// Blake family of hash functions
// BLAKE2S
//
// We only support 32byte (256 bit)
RegisterHashFunc(BLAKE2S_MIN+31, sumBlake2s32)
// BLAKE2B
for c := uint64(BLAKE2B_MIN); c <= BLAKE2B_MAX; c++ {
size := int(c - BLAKE2B_MIN + 1)
RegisterHashFunc(c, func(buf []byte, _ int) ([]byte, error) {
return sumBlake2b(buf, size)
})
}
}
func init() {
registerStdlibHashFuncs()
registerNonStdlibHashFuncs()
}
// RegisterHashFunc adds an entry to the package-level code -> hash func map.
// The hash function must return at least the requested number of bytes. If it
// returns more, the hash will be truncated.
func RegisterHashFunc(code uint64, hashFunc HashFunc) error {
if !ValidCode(code) {
return fmt.Errorf("code %v not valid", code)
}
_, ok := funcTable[code]
if ok {
return fmt.Errorf("hash func for code %v already registered", code)
}
funcTable[code] = hashFunc
return nil
}