docker-ipfs-keygen/main.go
Jim Pick 998f428f1e Add go.mod and update for libp2p-core
Fixes install problems.
2019-09-17 13:25:47 -07:00

54 lines
1.0 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"strings"
ci "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", "RSA", "select type of key to generate (RSA or Ed25519)")
flag.Parse()
var atyp int
switch strings.ToLower(*typ) {
case "rsa":
atyp = ci.RSA
case "ed25519":
atyp = ci.Ed25519
default:
fmt.Fprintln(os.Stderr, "unrecognized key type: ", *typ)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ)
priv, pub, err := ci.GenerateKeyPair(atyp, *size)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintln(os.Stderr, "Success!")
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)
}