-
Notifications
You must be signed in to change notification settings - Fork 0
/
pem.go
49 lines (42 loc) · 987 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package certcut
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
)
// PrivateKeyPEM converts a private key to PEM format.
func PrivateKeyPEM(key *rsa.PrivateKey) []byte {
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
return pem.EncodeToMemory(block)
}
// PublicKeyPEM converts a public key to PEM format.
func PublicKeyPEM(key *rsa.PublicKey) ([]byte, error) {
b, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return nil, err
}
block := &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: b,
}
return pem.EncodeToMemory(block), nil
}
// CertPEM converts a certificate to PEM format.
func CertPEM(b []byte) []byte {
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: b,
}
return pem.EncodeToMemory(block)
}
// CSRPEM converts a certificate signing request to PEM format.
func CSRPEM(b []byte) []byte {
block := &pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: b,
}
return pem.EncodeToMemory(block)
}