forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
64 lines (61 loc) · 1.76 KB
/
decode.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
package xpem
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
)
func DecodePublicKey(pemContent []byte) (publicKey *rsa.PublicKey, err error) {
block, _ := pem.Decode(pemContent)
if block == nil {
return nil, fmt.Errorf("pem.Decode(%s):pemContent decode error", pemContent)
}
switch block.Type {
case "CERTIFICATE":
pubKeyCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("x509.ParseCertificate(%s):%w", pemContent, err)
}
pubKey, ok := pubKeyCert.PublicKey.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("公钥证书提取公钥出错 [%s]", pemContent)
}
publicKey = pubKey
case "PUBLIC KEY":
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("x509.ParsePKIXPublicKey(%s),err:%w", pemContent, err)
}
pubKey, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("公钥解析出错 [%s]", pemContent)
}
publicKey = pubKey
case "RSA PUBLIC KEY":
pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("x509.ParsePKCS1PublicKey(%s):%w", pemContent, err)
}
publicKey = pubKey
}
return publicKey, nil
}
func DecodePrivateKey(pemContent []byte) (privateKey *rsa.PrivateKey, err error) {
block, _ := pem.Decode(pemContent)
if block == nil {
return nil, fmt.Errorf("pem.Decode(%s):pemContent decode error", pemContent)
}
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
pk8, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("私钥解析出错 [%s]", pemContent)
}
var ok bool
privateKey, ok = pk8.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("私钥解析出错 [%s]", pemContent)
}
}
return privateKey, nil
}