forked from kubernetes-retired/kube-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pem.go
33 lines (28 loc) · 714 Bytes
/
pem.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
package tlsutil
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
)
func EncodePrivateKeyPEM(key *rsa.PrivateKey) []byte {
block := pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
return pem.EncodeToMemory(&block)
}
func DecodePrivateKeyPEM(data []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(data)
return x509.ParsePKCS1PrivateKey(block.Bytes)
}
func EncodeCertificatePEM(cert *x509.Certificate) []byte {
block := pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
}
return pem.EncodeToMemory(&block)
}
func DecodeCertificatePEM(data []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(data)
return x509.ParseCertificate(block.Bytes)
}