-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash.go
215 lines (191 loc) · 4.76 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
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
package hash
import (
"bytes"
"crypto/sha256"
"hash"
// We include sha1 for git support.
"crypto/sha1" //nolint:gosec
b58 "github.com/mr-tron/base58/base58"
"github.com/pkg/errors"
"github.com/zeebo/blake3"
"golang.org/x/exp/slices"
)
// ErrHashMismatch is returned when hashes mismatch.
var ErrHashMismatch = errors.New("hash mismatch")
// ErrHashTypeUnknown is returned when the hash type is unknown.
var ErrHashTypeUnknown = errors.New("unknown hash type")
// SupportedHashTypes is the list of built-in hash types.
var SupportedHashTypes = []HashType{
HashType_HashType_SHA256,
HashType_HashType_SHA1,
HashType_HashType_BLAKE3,
}
// RecommendedHashType is the hash type recommended to use.
// Note: not guaranteed to stay the same between Bifrost versions.
const RecommendedHashType = HashType_HashType_BLAKE3
// UnmarshalHashJSON unmarshals a hash from json.
func UnmarshalHashJSON(data []byte) (*Hash, error) {
h := &Hash{}
if err := h.UnmarshalJSON(data); err != nil {
return nil, err
}
return h, nil
}
// IsNil checks if the object is nil.
func (h *Hash) IsNil() bool {
return h == nil
}
// IsEmpty checks if the hash is empty.
func (h *Hash) IsEmpty() bool {
return h.GetHashType() == 0 || len(h.GetHash()) == 0
}
// Clone clones the hash object.
func (h *Hash) Clone() *Hash {
if h == nil {
return nil
}
return &Hash{
HashType: h.GetHashType(),
Hash: slices.Clone(h.GetHash()),
}
}
// Validate validates the hash type.
func (h HashType) Validate() error {
switch h {
case HashType_HashType_UNKNOWN:
return nil
case HashType_HashType_SHA256:
return nil
case HashType_HashType_SHA1:
return nil
case HashType_HashType_BLAKE3:
return nil
default:
return errors.Errorf("hash type unknown: %v", h.String())
}
}
// GetHashLen returns the hash length.
func (h HashType) GetHashLen() int {
switch h {
case HashType_HashType_SHA256:
return sha256.Size
case HashType_HashType_SHA1:
return sha1.Size //nolint:gosec
case HashType_HashType_BLAKE3:
return 32
}
return 0
}
// Sum takes the sum with the hash type.
func (h HashType) Sum(data []byte) ([]byte, error) {
switch h {
case HashType_HashType_SHA256:
h := sha256.Sum256(data)
return h[:], nil
case HashType_HashType_SHA1:
h := sha1.Sum(data) //nolint:gosec
return h[:], nil
case HashType_HashType_BLAKE3:
h := blake3.Sum256(data)
return h[:], nil
default:
return nil, errors.Errorf("hash type unknown: %v", h.String())
}
}
// CompareHash compares two hashes.
func (h *Hash) CompareHash(other *Hash) bool {
if other == nil && h == nil {
return true
}
if h == nil || other == nil {
return false
}
if h.GetHashType() != other.GetHashType() {
return false
}
if len(h.GetHash()) != len(other.GetHash()) {
return false
}
if !bytes.Equal(h.GetHash(), other.GetHash()) {
return false
}
return true
}
// BuildHasher builds the hasher for the hash type.
func (h HashType) BuildHasher() (hash.Hash, error) {
switch h {
case HashType_HashType_SHA256:
return sha256.New(), nil
case HashType_HashType_SHA1:
return sha1.New(), nil //nolint:gosec
case HashType_HashType_BLAKE3:
return blake3.New(), nil
default:
return nil, errors.Errorf("hash type unknown: %v", h.String())
}
}
// VerifyData verifies data against the sum.
// Returns the hash of the data, hash type, and error
// Returns an error if failed to validate.
func (h *Hash) VerifyData(data []byte) ([]byte, error) {
hash, err := h.GetHashType().Sum(data)
if err != nil {
return nil, err
}
if len(hash) != len(h.GetHash()) {
return hash, ErrHashMismatch
}
if !bytes.Equal(hash, h.GetHash()) {
return hash, ErrHashMismatch
}
return hash, nil
}
// NewHash constructs a new hash object.
func NewHash(ht HashType, h []byte) *Hash {
return &Hash{HashType: ht, Hash: h}
}
// Sum constructs a hash type by summing an object.
func Sum(ht HashType, data []byte) (*Hash, error) {
h, err := ht.Sum(data)
if err != nil {
return nil, err
}
return NewHash(ht, h), nil
}
// Validate validates the hash.
func (h *Hash) Validate() error {
if err := h.GetHashType().Validate(); err != nil {
return err
}
if ehl := h.GetHashType().GetHashLen(); len(h.GetHash()) != ehl {
return errors.Errorf("expected hash length %d != %d", ehl, len(h.GetHash()))
}
return nil
}
// MarshalString marshals the hash to a string.
func (h *Hash) MarshalString() string {
if h == nil {
return ""
}
dat, err := h.MarshalVT()
if err != nil {
return ""
}
return b58.Encode(dat)
}
// MarshalDigest marshals the hash to a binary slice.
func (h *Hash) MarshalDigest() []byte {
if h == nil {
return nil
}
dat, _ := h.MarshalVT()
return dat
}
// ParseFromB58 parses the object ref from a base58 string.
func (h *Hash) ParseFromB58(ref string) error {
dat, err := b58.Decode(ref)
if err != nil {
return err
}
return h.UnmarshalVT(dat)
}