-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.go
206 lines (183 loc) · 4.05 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
package multihash
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"github.com/spaolacci/murmur3"
blake2b "golang.org/x/crypto/blake2b"
blake2s "golang.org/x/crypto/blake2s"
sha3 "golang.org/x/crypto/sha3"
keccak "leb.io/hashland/keccakpg"
)
// ErrSumNotSupported is returned when the Sum function code is not implemented
var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
// 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) {
m := Multihash{}
err := error(nil)
if !ValidCode(code) {
return m, fmt.Errorf("invalid multihash code %d", code)
}
if length < 0 {
var ok bool
length, ok = DefaultLengths[code]
if !ok {
return m, fmt.Errorf("no default length for code %d", code)
}
}
var d []byte
switch {
case isBlake2s(code):
olen := code - BLAKE2S_MIN + 1
switch olen {
case 32:
out := blake2s.Sum256(data)
d = out[:]
default:
return nil, fmt.Errorf("unsupported length for blake2s: %d", olen)
}
case isBlake2b(code):
olen := code - BLAKE2B_MIN + 1
switch olen {
case 32:
out := blake2b.Sum256(data)
d = out[:]
case 48:
out := blake2b.Sum384(data)
d = out[:]
case 64:
out := blake2b.Sum512(data)
d = out[:]
default:
return nil, fmt.Errorf("unsupported length for blake2b: %d", olen)
}
default:
switch code {
case ID:
d = sumID(data)
case SHA1:
d = sumSHA1(data)
case SHA2_256:
d = sumSHA256(data)
case SHA2_512:
d = sumSHA512(data)
case KECCAK_224:
d = sumKeccak224(data)
case KECCAK_256:
d = sumKeccak256(data)
case KECCAK_384:
d = sumKeccak384(data)
case KECCAK_512:
d = sumKeccak512(data)
case SHA3_224:
d = sumSHA3_224(data)
case SHA3_256:
d = sumSHA3_256(data)
case SHA3_384:
d = sumSHA3_384(data)
case SHA3_512:
d = sumSHA3_512(data)
case DBL_SHA2_256:
d = sumSHA256(sumSHA256(data))
case MURMUR3:
d, err = sumMURMUR3(data)
case SHAKE_128:
d = sumSHAKE128(data)
case SHAKE_256:
d = sumSHAKE256(data)
default:
return m, ErrSumNotSupported
}
}
if err != nil {
return m, err
}
return Encode(d[0:length], code)
}
func isBlake2s(code uint64) bool {
return code >= BLAKE2S_MIN && code <= BLAKE2S_MAX
}
func isBlake2b(code uint64) bool {
return code >= BLAKE2B_MIN && code <= BLAKE2B_MAX
}
func sumID(data []byte) []byte {
return data
}
func sumSHA1(data []byte) []byte {
a := sha1.Sum(data)
return a[0:20]
}
func sumSHA256(data []byte) []byte {
a := sha256.Sum256(data)
return a[0:32]
}
func sumSHA512(data []byte) []byte {
a := sha512.Sum512(data)
return a[0:64]
}
func sumKeccak224(data []byte) []byte {
h := keccak.New224()
h.Write(data)
return h.Sum(nil)
}
func sumKeccak256(data []byte) []byte {
h := keccak.New256()
h.Write(data)
return h.Sum(nil)
}
func sumKeccak384(data []byte) []byte {
h := keccak.New384()
h.Write(data)
return h.Sum(nil)
}
func sumKeccak512(data []byte) []byte {
h := keccak.New512()
h.Write(data)
return h.Sum(nil)
}
func sumSHA3(data []byte) ([]byte, error) {
h := sha3.New512()
if _, err := h.Write(data); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
func sumSHA3_512(data []byte) []byte {
a := sha3.Sum512(data)
return a[:]
}
func sumMURMUR3(data []byte) ([]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) []byte {
bytes := make([]byte, 32)
sha3.ShakeSum128(bytes, data)
return bytes
}
func sumSHAKE256(data []byte) []byte {
bytes := make([]byte, 64)
sha3.ShakeSum256(bytes, data)
return bytes
}
func sumSHA3_384(data []byte) []byte {
a := sha3.Sum384(data)
return a[:]
}
func sumSHA3_256(data []byte) []byte {
a := sha3.Sum256(data)
return a[:]
}
func sumSHA3_224(data []byte) []byte {
a := sha3.Sum224(data)
return a[:]
}