-
Notifications
You must be signed in to change notification settings - Fork 4
/
crypto.go
39 lines (31 loc) · 914 Bytes
/
crypto.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
package common
import (
"crypto"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"github.com/alphatr/acme-lego/common/errors"
)
// LoadPrivateKey 加载私钥
func LoadPrivateKey(file string) (crypto.PrivateKey, *errors.Error) {
keyBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.NewError(errors.CommonFileReadErrno, err, file)
}
keyBlock, _ := pem.Decode(keyBytes)
switch keyBlock.Type {
case "RSA PRIVATE KEY":
prevate, err := x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
if err != nil {
return nil, errors.NewError(errors.CommonParsePrivateErrno, err, "rsa")
}
return prevate, nil
case "EC PRIVATE KEY":
prevate, err := x509.ParseECPrivateKey(keyBlock.Bytes)
if err != nil {
return nil, errors.NewError(errors.CommonParsePrivateErrno, err, "ecc")
}
return prevate, nil
}
return nil, errors.NewError(errors.CommonUnknowBlockErrno, nil, keyBlock.Type)
}