/
file.go
65 lines (52 loc) · 1.38 KB
/
file.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
package simplecertify
import (
"crypto/x509"
"encoding/json"
"fmt"
"os"
)
var CERTIFIER_CONFIG_PATH = "certifier.json"
func Save(ca *Certifier) error {
raw := ca.Certificate.Raw
config := &CertifierConfig{
PrivateKey: x509.MarshalPKCS1PrivateKey(ca.PrivateKey),
Certificate: raw,
}
file, err := os.OpenFile(CERTIFIER_CONFIG_PATH, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
buf, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("error marshalling config: %v", err)
}
_, err = file.Write(buf)
if err != nil {
fmt.Println("error writing to file:", err)
return err
}
return nil
}
func Load() (*Certifier, error) {
file, err := os.Open(CERTIFIER_CONFIG_PATH)
if err != nil {
return nil, fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
config := &CertifierConfig{}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
return nil, fmt.Errorf("error decoding config: %v", err)
}
privateKey, err := x509.ParsePKCS1PrivateKey(config.PrivateKey)
if err != nil {
return nil, fmt.Errorf("error parsing private key: %v", err)
}
certificate, err := x509.ParseCertificate(config.Certificate)
if err != nil {
return nil, fmt.Errorf("error parsing certificate: %v", err)
}
return NewCertifier(privateKey, certificate), nil
}