-
Notifications
You must be signed in to change notification settings - Fork 3
/
parsePem.go
57 lines (51 loc) · 1.41 KB
/
parsePem.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 rsaKit
import (
"crypto/rsa"
"github.com/golang-jwt/jwt/v5"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
)
// ParsePublicKeyFromPem 解析公钥.
/*
支持: PKCS1、PKCS8.
*/
func ParsePublicKeyFromPem(pemData []byte) (*rsa.PublicKey, error) {
return jwt.ParseRSAPublicKeyFromPEM(pemData)
//block, _ := pem.Decode(pemData)
//if block == nil {
// return nil, errorKit.New("fail to decode pem because block is nil")
//}
//
//keyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
//if err != nil {
// return nil, err
//}
//return keyInterface.(*rsa.PublicKey), nil
}
// ParsePrivateKeyFromPem 解析私钥.
/*
支持: PKCS1、PKCS8.
@param password 私钥的密码("": 无密码)
*/
func ParsePrivateKeyFromPem(data []byte, password string) (*rsa.PrivateKey, error) {
if strKit.IsNotEmpty(password) {
return jwt.ParseRSAPrivateKeyFromPEMWithPassword(data, password)
}
return jwt.ParseRSAPrivateKeyFromPEM(data)
//block, _ := pem.Decode(data)
//if block == nil {
// return nil, errorKit.New("fail to decode pem because block is nil")
//}
//
//switch opts.format {
//case PKCS1:
// return x509.ParsePKCS1PrivateKey(block.Bytes)
//case PKCS8:
// keyInterface, err := x509.ParsePKCS8PrivateKey(block.Bytes)
// if err != nil {
// return nil, err
// }
// return keyInterface.(*rsa.PrivateKey), nil
//default:
// return nil, errorKit.New("invalid key format(%d)", opts.format)
//}
}