-
Notifications
You must be signed in to change notification settings - Fork 3
/
rsa.go
58 lines (49 loc) · 1.16 KB
/
rsa.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
package main
import (
"crypto/rand"
"crypto/rsa"
"encoding/gob"
"fmt"
"log"
"os"
)
// Writes private key to file which will be placed in current
// directory.
func writePrivKey(priv *rsa.PrivateKey) {
privFile := "key.priv"
f, err := os.Create(privFile)
if err != nil {
log.Fatalf("Could not write to file: %v", err)
}
defer f.Close()
w := gob.NewEncoder(f)
w.Encode(priv)
}
// Writes public key to file which will be placed in current
// directory.
func writePubKey(priv *rsa.PrivateKey) {
pubFile := "key.pub"
f, err := os.Create(pubFile)
if err != nil {
log.Fatalf("Could not write to file: %v", err)
}
defer f.Close()
fmt.Printf("[!] Paste this public key in main: %v ", priv.Public())
fmt.Println("\n\n[+] Copy of public key written to file.")
w := gob.NewEncoder(f)
w.Encode(priv.Public())
}
func main() {
// Generates private/public key pair.
rnd := rand.Reader
Priv, err := rsa.GenerateKey(rnd, 2048)
if err != nil {
log.Println(err)
}
// The following functions will write
// the private/public key to their
// respective files.
writePrivKey(Priv)
writePubKey(Priv)
fmt.Println("[+] Private/Public key pair written to file.")
}