-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.go
63 lines (49 loc) · 1.2 KB
/
save.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
// Copyright 2020 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
)
func getCerts(gen bool) ([]byte, []byte, error) {
if gen {
return nil, nil, errors.New("generate key")
}
exePath, err := os.Executable()
if err != nil {
return nil, nil, err
}
exePath = filepath.Dir(exePath)
content, err := ioutil.ReadFile(filepath.Join(exePath, "friendly.cert"))
if err != nil {
return nil, nil, err
}
certs := map[string]string{}
err = json.Unmarshal(content, &certs)
if err != nil {
return nil, nil, err
}
return []byte(certs["public"]), []byte(certs["private"]), nil
}
func saveCerts(public []byte, private []byte) error {
exePath, err := os.Executable()
if err != nil {
return err
}
exePath = filepath.Dir(exePath)
data, _ := json.Marshal(map[string]string{
"private": string(private),
"public": string(public),
})
return ioutil.WriteFile(filepath.Join(exePath, "friendly.cert"), data, 0644)
}
func deleteCerts() error {
exePath, err := os.Executable()
if err != nil {
return err
}
exePath = filepath.Dir(exePath)
return os.Remove(filepath.Join(exePath, "friendly.cert"))
}