-
Notifications
You must be signed in to change notification settings - Fork 12
/
keyring.go
62 lines (50 loc) · 1.28 KB
/
keyring.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
package swim
import (
"os"
"github.com/DE-labtory/heimdall"
"github.com/DE-labtory/heimdall/config"
"github.com/DE-labtory/heimdall/hecdsa"
"github.com/DE-labtory/iLogger"
)
/*
secLV
128 -> P256
192 -> P384
256 -> P521
*/
// if the key already exists, create a new one, or load the existing key.
func GenerateKey(keyPath string, SecLv int) (heimdall.PriKey, heimdall.PubKey) {
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
pri, pub := GenerateNewKey(keyPath, SecLv)
return pri, pub
}
pri, pub := LoadKeyPair(keyPath)
return pri, pub
}
func GenerateNewKey(keyPath string, SecLv int) (heimdall.PriKey, heimdall.PubKey) {
myConFig, err := config.NewSimpleConfig(SecLv)
if err != nil {
iLogger.Panic(nil, err.Error())
}
keyGenOpt := myConFig.KeyGenOpt
pri, err := hecdsa.GenerateKey(keyGenOpt)
if err != nil {
iLogger.Panic(nil, err.Error())
}
err = hecdsa.StorePriKeyWithoutPwd(pri, keyPath)
if err != nil {
iLogger.Panic(nil, err.Error())
}
return pri, pri.PublicKey()
}
func LoadKeyPair(keyPath string) (heimdall.PriKey, heimdall.PubKey) {
pri, err := hecdsa.LoadPriKeyWithoutPwd(keyPath)
if err != nil {
iLogger.Panic(nil, err.Error())
}
return pri, pri.PublicKey()
}
func GetNodeID(keyPath string) string {
pri, _ := LoadKeyPair(keyPath)
return pri.ID()
}