-
Notifications
You must be signed in to change notification settings - Fork 31
/
algorithm.go
49 lines (41 loc) · 1.04 KB
/
algorithm.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
package httpsignature
import (
"errors"
)
// Algorithm is an enum-like representing an algorithm that can be used for http signatures
type Algorithm int
const (
invalid Algorithm = iota
// ED25519 EdDSA - deprecated, all algorithm strings should be replaced with HS2019
ED25519
// HS2019 is a catch-all value for all algorithms
HS2019
)
var algorithmName = map[Algorithm]string{
ED25519: "ed25519",
HS2019: "hs2019",
}
var algorithmID = map[string]Algorithm{
"ed25519": ED25519,
"hs2019": HS2019,
}
func (a Algorithm) String() string {
return algorithmName[a]
}
// MarshalText marshalls the algorithm into text.
func (a *Algorithm) MarshalText() (text []byte, err error) {
if *a == invalid {
return nil, errors.New("not a supported algorithm")
}
text = []byte(a.String())
return
}
// UnmarshalText unmarshalls the algorithm from text.
func (a *Algorithm) UnmarshalText(text []byte) (err error) {
var exists bool
*a, exists = algorithmID[string(text)]
if !exists {
return errors.New("not a supported algorithm")
}
return nil
}