-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
46 lines (39 loc) · 878 Bytes
/
main.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"log"
"os"
)
const (
logFmt = "%s\nError: %s"
privFile = "rsa2048PKCS1Priv.pem"
pubFile = "rsa2048PKCS1Pub.pem"
)
func main() {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatalf(logFmt, "Failed to generate RSA key.", err)
}
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}
out := pem.EncodeToMemory(block)
err = os.WriteFile(privFile, out, 0644)
if err != nil {
log.Fatalf(logFmt, "Failed to write RSA private key.", err)
}
pub := &priv.PublicKey
block = &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(pub),
}
out = pem.EncodeToMemory(block)
err = os.WriteFile(pubFile, out, 0644)
if err != nil {
log.Fatalf(logFmt, "Failed to write RSA public key.", err)
}
}