-
Notifications
You must be signed in to change notification settings - Fork 5
/
loader.go
49 lines (38 loc) · 945 Bytes
/
loader.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
package jose
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
)
// RSAPrivateKey parses data as *rsa.PrivateKey
func RSAPrivateKey(data []byte) (*rsa.PrivateKey, error) {
input := pemDecode(data)
var err error
var key interface{}
if key, err = x509.ParsePKCS1PrivateKey(input); err != nil {
if key, err = x509.ParsePKCS8PrivateKey(input); err != nil {
return nil, err
}
}
return key.(*rsa.PrivateKey), nil
}
// RSAPublicKey parses data as *rsa.PublicKey
func RSAPublicKey(data []byte) (*rsa.PublicKey, error) {
input := pemDecode(data)
var err error
var key interface{}
if key, err = x509.ParsePKIXPublicKey(input); err != nil {
if cert, err := x509.ParseCertificate(input); err == nil {
key = cert.PublicKey
} else {
return nil, err
}
}
return key.(*rsa.PublicKey), nil
}
func pemDecode(data []byte) []byte {
if block, _ := pem.Decode(data); block != nil {
return block.Bytes
}
return data
}