-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmain.go
110 lines (91 loc) · 2.32 KB
/
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
crp "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
)
func main() {
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
typ := flag.String("type", "", "select type of key to generate (RSA, Ed25519, Secp256k1 or ECDSA)")
key := flag.String("key", "", "specify the location of the key to decode it's peerID")
flag.Parse()
if *key != "" {
if err := readKey(key, typ); err != nil {
fmt.Fprintln(os.Stderr, err)
}
return
}
if *typ == "" {
*typ = "RSA"
}
if err := genKey(typ, size); err != nil {
fmt.Fprintln(os.Stderr, err)
}
return
}
func readKey(keyLoc *string, typ *string) error {
data, err := ioutil.ReadFile(*keyLoc)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc)
var unmarshalPrivateKeyFucn func(data []byte) (crp.PrivKey, error)
// rsa and ed25519 unmarshalPrivateKeyFucn are for backward compatibility
// for keys saved with raw(), to read such keys, specify the key type
switch strings.ToLower(*typ) {
case "rsa":
unmarshalPrivateKeyFucn = crp.UnmarshalRsaPrivateKey
case "ed25519":
unmarshalPrivateKeyFucn = crp.UnmarshalEd25519PrivateKey
default:
unmarshalPrivateKeyFucn = crp.UnmarshalPrivateKey
}
prvk, err := unmarshalPrivateKeyFucn(data)
if err != nil {
return err
}
id, err := peer.IDFromPrivateKey(prvk)
if err != nil {
return err
}
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvk.Type().String(), id.Pretty())
return err
}
func genKey(typ *string, size *int) error {
var atyp int
switch strings.ToLower(*typ) {
case "rsa":
atyp = crp.RSA
case "ed25519":
atyp = crp.Ed25519
case "secp256k1":
atyp = crp.Secp256k1
case "ecdsa":
atyp = crp.ECDSA
default:
return fmt.Errorf("unrecognized key type: %s", *typ)
}
fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ)
priv, pub, err := crp.GenerateKeyPair(atyp, *size)
if err != nil {
return err
}
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
return err
}
data, err := crp.MarshalPrivateKey(priv)
if err != nil {
return err
}
_, err = os.Stdout.Write(data)
if err != nil {
return nil
}
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for generated key: %s\n", pid.Pretty())
return err
}