-
Notifications
You must be signed in to change notification settings - Fork 175
/
hash.go
80 lines (70 loc) · 2.16 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
package crypto
import (
"errors"
"fmt"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/flow"
)
// prefixedHashing embeds a crypto hasher and implements
// hashing with a prefix : prefixedHashing(data) = hasher(prefix || data)
//
// Prefixes are padded tags till 32 bytes to guarantee prefixedHashers are independant
// hashers.
// Prefixes are disabled with the particular tag value ""
type prefixedHashing struct {
hash.Hasher
usePrefix bool
tag [flow.DomainTagLength]byte
}
// paddedDomainTag converts a string into a padded byte array.
func paddedDomainTag(s string) ([flow.DomainTagLength]byte, error) {
var tag [flow.DomainTagLength]byte
if len(s) > flow.DomainTagLength {
return tag, fmt.Errorf("domain tag %s cannot be longer than %d characters", s, flow.DomainTagLength)
}
copy(tag[:], s)
return tag, nil
}
// NewPrefixedHashing returns a new hasher that prefixes the tag for all
// hash computations (only when tag is not empty).
// Only SHA2 and SHA3 algorithms are supported.
func NewPrefixedHashing(shaAlgo hash.HashingAlgorithm, tag string) (hash.Hasher, error) {
var hasher hash.Hasher
switch shaAlgo {
case hash.SHA2_256:
hasher = hash.NewSHA2_256()
case hash.SHA3_256:
hasher = hash.NewSHA3_256()
case hash.SHA2_384:
hasher = hash.NewSHA2_384()
case hash.SHA3_384:
hasher = hash.NewSHA3_384()
default:
return nil, errors.New("hashing algorithm is not a supported for prefixed algorithm")
}
paddedTag, err := paddedDomainTag(tag)
if err != nil {
return nil, fmt.Errorf("prefixed hashing failed: %w", err)
}
return &prefixedHashing{
Hasher: hasher,
// if tag is empty, do not use any prefix (standard hashing)
usePrefix: tag != "",
tag: paddedTag,
}, nil
}
// ComputeHash calculates and returns the digest of input byte array prefixed by a tag.
// Not thread-safe
func (s *prefixedHashing) ComputeHash(data []byte) hash.Hash {
s.Reset()
_, _ = s.Write(data)
return s.Hasher.SumHash()
}
// Reset gets the hasher back to its original state.
func (s *prefixedHashing) Reset() {
s.Hasher.Reset()
// include the tag only when using a prefix is enabled
if s.usePrefix {
_, _ = s.Write(s.tag[:])
}
}