-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.go
More file actions
195 lines (173 loc) · 5.59 KB
/
algorithm.go
File metadata and controls
195 lines (173 loc) · 5.59 KB
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
package digest
import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"regexp"
"sync"
)
// Algorithm specifies an algorithm used to generate a digest.
type Algorithm struct {
name string
}
// algorithmInfo contains the registered data per algorithm.
// They each have a name, size, encoder, and a hash function.
type algorithmInfo struct {
name string
size int
enc Encoder
newFn func() hash.Hash
}
var (
algorithms = map[string]algorithmInfo{}
algorithmsMu sync.RWMutex
algorithmRegexp = regexp.MustCompile(`^[a-z0-9]+([+._-][a-z0-9]+)*$`)
Canonical Algorithm // Canonical is the default hashing algorithm, currently set to [SHA256].
SHA256 Algorithm // SHA256 defines the registered sha256 digester based on [crypto/sha256].
SHA512 Algorithm // SHA512 defines the registered sha512 digester based on [crypto/sha512].
aiCanonical algorithmInfo
aiSHA256 algorithmInfo
aiSHA512 algorithmInfo
)
func init() {
// Ignore errors, do not panic.
// Predefined algorithms would be invalid if they cannot be registered for some reason.
aiSHA256, SHA256, _ = algorithmInfoRegister("sha256", EncodeHex{Len: 64}, sha256.New)
aiSHA512, SHA512, _ = algorithmInfoRegister("sha512", EncodeHex{Len: 128}, sha512.New)
Canonical = SHA256
aiCanonical = aiSHA256
}
// AlgorithmLookup is used to get a previously registered [Algorithm].
func AlgorithmLookup(name string) (Algorithm, error) {
_, a, err := algorithmInfoLookup(name)
return a, err
}
func algorithmInfoLookup(name string) (algorithmInfo, Algorithm, error) {
// skip the lock for registered algorithms
switch name {
case "sha256":
return aiSHA256, SHA256, nil
case "sha512":
return aiSHA512, SHA512, nil
case "":
return algorithmInfo{}, Algorithm{}, ErrAlgorithmInvalidName
}
algorithmsMu.RLock()
defer algorithmsMu.RUnlock()
if a, ok := algorithms[name]; ok {
return a, Algorithm{name: a.name}, nil
}
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrAlgorithmUnknown, name)
}
// AlgorithmRegister is used to register new hash algorithms.
// Attempting to register an already registered algorithm will fail.
// The name must follow the regexp "[a-z0-9]+([+._-][a-z0-9]+)*".
// The encoder and hash function are also verified to be valid interfaces.
func AlgorithmRegister(name string, enc Encoder, newFn func() hash.Hash) (Algorithm, error) {
_, a, err := algorithmInfoRegister(name, enc, newFn)
return a, err
}
func algorithmInfoRegister(name string, enc Encoder, newFn func() hash.Hash) (algorithmInfo, Algorithm, error) {
algorithmsMu.Lock()
defer algorithmsMu.Unlock()
if _, ok := algorithms[name]; ok {
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrAlgorithmExists, name)
}
if !algorithmRegexp.MatchString(name) {
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrAlgorithmInvalidName, name)
}
if enc == nil {
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrEncodeInterfaceInvalid, name)
}
if newFn == nil {
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrHashFunctionInvalid, name)
}
hasher := newFn()
if hasher == nil {
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrHashFunctionInvalid, name)
}
size := hasher.Size()
if size <= 0 {
return algorithmInfo{}, Algorithm{}, fmt.Errorf("%w: %s", ErrHashFunctionInvalid, name)
}
alg := algorithmInfo{
name: name,
size: size,
enc: enc,
newFn: newFn,
}
algorithms[name] = alg
return alg, Algorithm{name: name}, nil
}
// Digester creates a new [Digester] for the algorithm.
func (a Algorithm) Digester() (Digester, error) {
if a.name == "" {
return nil, ErrAlgorithmInvalidName
}
return NewWriter(nil, a), nil
}
// Encode converts the byte slice hash sum to an encoded string for a digest.
func (a Algorithm) Encode(p []byte) (string, error) {
ai, _, err := algorithmInfoLookup(a.name)
if err != nil {
return "", err
}
return ai.enc.Encode(p)
}
// Equal returns true if the algorithms are the same.
func (a Algorithm) Equal(cmp Algorithm) bool {
return a.name == cmp.name
}
// FromBytes generates a digest on the input byte slice using the algorithm and returns a [Digest].
// This will fail if the algorithm is invalid.
func (a Algorithm) FromBytes(p []byte) (Digest, error) {
dr, err := a.Digester()
if err != nil {
return Digest{}, err
}
if _, err := dr.Write(p); err != nil {
return Digest{}, err
}
return dr.Digest()
}
// FromReader generates a digest on the input reader using the algorithm and returns a [Digest].
// This will fail if the algorithm is invalid or on read errors.
func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
dr, err := a.Digester()
if err != nil {
return Digest{}, err
}
if _, err := io.Copy(dr, rd); err != nil {
return Digest{}, err
}
return dr.Digest()
}
// FromString generates a digest on the input string using the algorithm and returns a [Digest].
// This will fail if the algorithm is invalid.
func (a Algorithm) FromString(s string) (Digest, error) {
return a.FromBytes([]byte(s))
}
// Hash returns a new [hash.Hash] for the algorithm.
// nil is returned if the algorithm is invalid.
func (a Algorithm) Hash() hash.Hash {
ai, _, err := algorithmInfoLookup(a.name)
if err != nil {
return nil
}
return ai.newFn()
}
// IsZero returns true if the algorithm is the zero value.
func (a Algorithm) IsZero() bool {
return a.name == ""
}
// Size returns the detected output byte size of the hash implementation.
func (a Algorithm) Size() int {
ai, _, _ := algorithmInfoLookup(a.name)
return ai.size
}
// String returns the name of the digest algorithm.
func (a Algorithm) String() string {
return a.name
}