-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
94 lines (82 loc) · 1.55 KB
/
keys.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
package mvpm
import (
"crypto/subtle"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
)
type NamedKeySet struct {
Keys map[string][]byte
ActiveKeyName string
}
func (ks *NamedKeySet) ActiveKey() []byte {
return ks.Keys[ks.ActiveKeyName]
}
func ParseKeys(s string) ([][]byte, error) {
var keys [][]byte
for _, ks := range strings.FieldsFunc(s, isWhitespaceOrComma) {
if ks == "" {
continue
}
key, err := hex.DecodeString(ks)
if err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
}
type Keys [][]byte
func KeysVar(v *[][]byte) *Keys {
return (*Keys)(v)
}
func (v Keys) String() string {
var buf strings.Builder
for i, k := range v {
if i > 0 {
buf.WriteByte(' ')
}
buf.WriteString(hex.EncodeToString(k))
}
return buf.String()
}
func (v Keys) Get() interface{} {
return [][]byte(v)
}
func (v *Keys) Set(raw string) (err error) {
*v, err = ParseKeys(raw)
return
}
func (v *Keys) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
if b[0] == '[' {
var strs []string
if err := json.Unmarshal(b, &strs); err != nil {
return err
}
*v = make(Keys, 0, len(strs))
for _, str := range strs {
key, err := hex.DecodeString(str)
if err != nil {
return err
}
*v = append(*v, key)
}
return nil
}
return fmt.Errorf("keys must be an array")
}
func (v Keys) Match(b []byte) bool {
for _, k := range v {
if 1 == subtle.ConstantTimeCompare(k, b) {
return true
}
}
return false
}
func (v Keys) MatchString(str string) bool {
return v.Match([]byte(str))
}