From 169c839295d0f7ad25383f8e5b92629f017b2173 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sun, 23 Sep 2018 15:05:11 +0300 Subject: [PATCH] p2p-keygen: key generator for persistent identities --- identity.go | 2 +- p2p-keygen/main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 p2p-keygen/main.go diff --git a/identity.go b/identity.go index d60360f..e33b437 100644 --- a/identity.go +++ b/identity.go @@ -21,5 +21,5 @@ func WriteIdentity(k crypto.PrivKey, path string) error { return err } - return ioutil.WriteFile(path, bytes, 0500) + return ioutil.WriteFile(path, bytes, 0400) } diff --git a/p2p-keygen/main.go b/p2p-keygen/main.go new file mode 100644 index 0000000..830291b --- /dev/null +++ b/p2p-keygen/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "flag" + "fmt" + "log" + + p2pd "github.com/libp2p/go-libp2p-daemon" + + crypto "github.com/libp2p/go-libp2p-crypto" + peer "github.com/libp2p/go-libp2p-peer" +) + +func main() { + file := flag.String("f", "identity", "output key file") + ktype := flag.String("t", "rsa", "key type; rsa or ed25519") + bits := flag.Int("b", 2048, "key size in bits (for rsa)") + flag.Parse() + + var typ int + + switch *ktype { + case "rsa": + typ = crypto.RSA + + case "ed25519": + typ = crypto.Ed25519 + + default: + log.Fatalf("Unknown key type %s; must be rsa or ed25519", *ktype) + } + + priv, pub, err := crypto.GenerateKeyPair(typ, *bits) + if err != nil { + log.Fatal(err) + } + + id, err := peer.IDFromPublicKey(pub) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Peer ID: %s\n", id.Pretty()) + + err = p2pd.WriteIdentity(priv, *file) + if err != nil { + log.Fatal(err) + } +}