forked from whyrusleeping/ipfs-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (61 loc) · 1.4 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
package main
import (
"flag"
"fmt"
"os"
"regexp"
ci "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
)
func main() {
vanity := flag.String("name", "", "write the name to appear in the begining of the key")
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
typ := flag.String("type", "RSA", "select type of key to generate")
flag.Parse()
var atyp int
switch *typ {
case "RSA":
atyp = ci.RSA
default:
fmt.Fprintln(os.Stderr, "unrecognized key type: ", *typ)
os.Exit(1)
}
r, err := regexp.Compile("^Qm" + *vanity + ".+$")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Generating some %d bit %s keys...\n", *size, *typ)
var priv ci.PrivKey
var pub ci.PubKey
var id = ""
var i = 0
for !r.MatchString(id) {
priv, pub, err = ci.GenerateKeyPair(atyp, *size)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
id = pid.Pretty()
i++
if i % 10 == 0 {
fmt.Fprintf(os.Stderr, "%s", ".")
}
if i % 500 == 0 {
fmt.Fprintln(os.Stderr, "", i)
}
}
fmt.Fprintln(os.Stderr, "\nSuccess!")
fmt.Fprintf(os.Stderr, "ID for generated key: %s\n", id)
data, err := priv.Bytes()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Stdout.Write(data)
}