2016-01-22 12:22:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-12-13 14:14:21 +00:00
|
|
|
"strings"
|
2016-01-22 12:22:54 +00:00
|
|
|
|
2016-12-23 19:53:54 +00:00
|
|
|
peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
|
|
|
|
ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
|
2016-01-22 12:22:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
|
2016-12-13 14:01:23 +00:00
|
|
|
typ := flag.String("type", "RSA", "select type of key to generate (RSA or Ed25519)")
|
2016-01-22 12:22:54 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
var atyp int
|
2016-12-13 14:07:39 +00:00
|
|
|
switch strings.ToLower(*typ) {
|
|
|
|
case "rsa":
|
2016-01-22 12:22:54 +00:00
|
|
|
atyp = ci.RSA
|
2016-12-13 14:07:39 +00:00
|
|
|
case "ed25519":
|
2016-12-13 14:01:23 +00:00
|
|
|
atyp = ci.Ed25519
|
2016-01-22 12:22:54 +00:00
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr, "unrecognized key type: ", *typ)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-02-05 20:50:07 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ)
|
2016-01-22 12:22:54 +00:00
|
|
|
priv, pub, err := ci.GenerateKeyPair(atyp, *size)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-02-05 20:50:07 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Success!")
|
2016-01-22 12:22:54 +00:00
|
|
|
|
|
|
|
pid, err := peer.IDFromPublicKey(pub)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "ID for generated key: %s\n", pid.Pretty())
|
|
|
|
|
|
|
|
data, err := priv.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Stdout.Write(data)
|
|
|
|
}
|