-
Notifications
You must be signed in to change notification settings - Fork 0
/
keytable.go
77 lines (67 loc) · 1.38 KB
/
keytable.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
package stdserver
import (
"crypto/ecdsa"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/golang-jwt/jwt/v4"
)
const (
ext = ".pem"
)
type KeyEntry struct {
Id string
Key interface{}
}
type KeyTable struct {
entries []KeyEntry
sign map[string]interface{}
verify map[string]interface{}
}
func NewKeyTable() *KeyTable {
return &KeyTable{
entries: make([]KeyEntry, 0),
sign: make(map[string]interface{}),
verify: make(map[string]interface{}),
}
}
func (k *KeyTable) PutECKey(id string, key *ecdsa.PrivateKey) {
k.entries = append(k.entries, KeyEntry{
Id: id,
Key: key,
})
k.sign[id] = key
k.verify[id] = &key.PublicKey
}
func (k *KeyTable) GetPrivateKeys() map[string]interface{} {
return k.sign
}
func (k *KeyTable) GetPublicKeys() map[string]interface{} {
return k.verify
}
func LoadKeyTableFromDir(root string) (*KeyTable, error) {
k := NewKeyTable()
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(path) == ext {
dat, err := ioutil.ReadFile(path)
if err != nil {
return err
}
key, err := jwt.ParseECPrivateKeyFromPEM(dat)
if err != nil {
return err
}
name := info.Name()
id := name[:strings.LastIndex(name, ext)]
k.PutECKey(id, key)
}
return nil
}); err != nil {
return nil, err
}
return k, nil
}