-
Notifications
You must be signed in to change notification settings - Fork 0
/
genCerts.go
56 lines (53 loc) · 1.25 KB
/
genCerts.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
)
func main() {
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
publicKey := &privateKey.PublicKey
// dump private key to file
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
fmt.Printf("error when marshaling private key: %s \n", err)
os.Exit(1)
}
privateKeyBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem, err := os.Create("private.pem")
if err != nil {
fmt.Printf("error when create private.pem: %s \n", err)
os.Exit(1)
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
fmt.Printf("error when encode private pem: %s \n", err)
os.Exit(1)
}
// dump public key to file
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
fmt.Printf("error when dumping publickey: %s \n", err)
os.Exit(1)
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem, err := os.Create("public.pem")
if err != nil {
fmt.Printf("error when create public.pem: %s \n", err)
os.Exit(1)
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
fmt.Printf("error when encode public pem: %s \n", err)
os.Exit(1)
}
}