-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsadkey.go
75 lines (60 loc) · 1.1 KB
/
sadkey.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
// Sadkey is a command line tool for generating RSA public and
// private keys
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"os"
)
func main() {
arg := flag.String("keygen", "", "sadkey -keygen rsa")
flag.Parse()
args := os.Args[:]
if len(args) <= 1 {
flag.Usage()
return
}
if *arg == "rsa" {
generateRSAKeyPairs()
fmt.Println("keys generated")
return
} else {
flag.Usage()
}
}
func generateRSAKeyPairs() {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
return
}
privPemBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}
f, err := os.Create("privPemFile.pem")
if err != nil {
fmt.Println(err)
return
}
pr := pem.EncodeToMemory(privPemBlock)
f.Write(pr)
f.Close()
pub := &priv.PublicKey
pubPemBlock := &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(pub),
}
nf, err := os.Create("pubPemFile.pem")
if err != nil {
fmt.Println(err)
return
}
ppr := pem.EncodeToMemory(pubPemBlock)
nf.Write(ppr)
nf.Close()
}