-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivateKey.go
57 lines (49 loc) · 1.42 KB
/
privateKey.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
package certgo
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"github.com/Alonza0314/cert-go/util"
logger "github.com/Alonza0314/logger-go"
)
func CreatePrivateKey(keyPath string) (*ecdsa.PrivateKey, error) {
logger.Info("CreatePrivateKey", "creating private key")
// check if private key exists
if util.FileExists(keyPath) {
logger.Warn("CreatePrivateKey", "private key already exists")
return nil, errors.New("private key already exists")
}
// generate private key
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
logger.Error("CreatePrivateKey", err.Error())
return nil, err
}
// encode private key
keyBytes, err := x509.MarshalECPrivateKey(privateKey)
if err != nil {
logger.Error("CreatePrivateKey", err.Error())
return nil, err
}
keyPEM := pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: keyBytes,
})
// check directory exists
if !util.FileDirExists(keyPath) {
logger.Warn("CreatePrivateKey", util.FileDir(keyPath)+" directory not exists, creating...")
if err := util.FileDirCreate(keyPath); err != nil {
return nil, err
}
logger.Info("CreatePrivateKey", util.FileDir(keyPath)+" directory created")
}
// save private key
if err := util.FileWrite(keyPath, keyPEM, 0644); err != nil {
return nil, err
}
logger.Info("CreatePrivateKey", "private key created")
return privateKey, nil
}