p2p-keygen: key generator for persistent identities

This commit is contained in:
vyzo 2018-09-23 15:05:11 +03:00
parent 55b32dbfdc
commit 169c839295
2 changed files with 49 additions and 1 deletions

View File

@ -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)
}

48
p2p-keygen/main.go Normal file
View File

@ -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)
}
}