forked from smartwalle/alipay
/
pkcs1.go
43 lines (36 loc) · 826 Bytes
/
pkcs1.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
package encoding
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)
func ParsePKCS1PrivateKey(data []byte) (key *rsa.PrivateKey, err error) {
var block *pem.Block
block, _ = pem.Decode(data)
if block == nil {
return nil, errors.New("private key error")
}
key, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return key, err
}
func ParsePKCS1PublicKey(data []byte) (key *rsa.PublicKey, err error) {
var block *pem.Block
block, _ = pem.Decode(data)
if block == nil {
return nil, errors.New("public key error")
}
var pubInterface interface{}
pubInterface, err = x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
key, ok := pubInterface.(*rsa.PublicKey)
if !ok {
return nil, errors.New("public key error")
}
return key, err
}