-
Notifications
You must be signed in to change notification settings - Fork 2
/
kms.go
92 lines (71 loc) · 1.48 KB
/
kms.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
package main
import (
"encoding/json"
"io"
"os"
"sync"
log "github.com/sirupsen/logrus"
)
var (
KeyStore map[string]string
KeyLock sync.Mutex
)
func KeyInitialize() {
KeyLock.Lock()
defer KeyLock.Unlock()
KeyStore = make(map[string]string)
}
func KeyLookup(key string) (string, bool) {
KeyLock.Lock()
defer KeyLock.Unlock()
value, found := KeyStore[key]
return value, found
}
func KeyAdd(public string, private string) {
KeyLock.Lock()
defer KeyLock.Unlock()
KeyStore[public] = private
}
func KeyDelete(key string) {
KeyLock.Lock()
defer KeyLock.Unlock()
delete(KeyStore, key)
}
func KeySave() error {
KeyLock.Lock()
defer KeyLock.Unlock()
file, err := os.OpenFile(GetDataPath()+"keys.keys", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if file != nil {
defer file.Close()
}
if err != nil {
log.Errorf("Error opening keys.keys for write: %v", err)
return err
}
bytes, err := json.Marshal(KeyStore)
if err != nil {
log.Errorf("Error marshalling json: %v", err)
}
_, err = file.Write(bytes)
return err
}
func KeyLoad() error {
KeyLock.Lock()
defer KeyLock.Unlock()
file, err := os.Open(GetDataPath() + "keys.keys")
if err != nil {
log.Errorf("Error opening keys.keys for read: %v", err)
return err
}
bytes, err := io.ReadAll(file)
file.Close()
if err != nil {
log.Errorf("Error reading keys.keys: %v", err)
return err
}
err = json.Unmarshal(bytes, &KeyStore)
if err != nil {
log.Errorf("Error unmarshalling json: %v", err)
}
return err
}