Commit 46b32041 authored by Marten Seemann's avatar Marten Seemann

add a command line flag to set the key type

parent 7b8ba831
......@@ -23,22 +23,41 @@ func main() {
func startClient() error {
port := flag.Int("p", 5533, "port")
peerIDString := flag.String("id", "", "peer ID")
keyType := flag.String("key", "ecdsa", "rsa, ecdsa, ed25519 or secp256k1")
flag.Parse()
peerID, err := peer.IDB58Decode(*peerIDString)
var priv ic.PrivKey
var err error
switch *keyType {
case "rsa":
fmt.Printf("Generated new peer with an RSA key.")
priv, _, err = ic.GenerateRSAKeyPair(2048, rand.Reader)
case "ecdsa":
fmt.Printf("Generated new peer with an ECDSA key.")
priv, _, err = ic.GenerateECDSAKeyPair(rand.Reader)
case "ed25519":
fmt.Printf("Generated new peer with an Ed25519 key.")
priv, _, err = ic.GenerateEd25519Key(rand.Reader)
case "secp256k1":
fmt.Printf("Generated new peer with an Secp256k1 key.")
priv, _, err = ic.GenerateSecp256k1Key(rand.Reader)
default:
return fmt.Errorf("unknown key type: %s", *keyType)
}
if err != nil {
return err
}
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader)
peerID, err := peer.IDB58Decode(*peerIDString)
if err != nil {
return err
}
id, err := peer.IDFromPrivateKey(priv)
if err != nil {
return err
}
fmt.Printf("Generated new peer with an ECDSA key. Peer ID: %s\n", id.Pretty())
fmt.Printf(" Peer ID: %s\n", id.Pretty())
tp, err := libp2ptls.New(priv)
if err != nil {
return err
......
......@@ -21,17 +21,36 @@ func main() {
func startServer() error {
port := flag.Int("p", 5533, "port")
keyType := flag.String("key", "ecdsa", "rsa, ecdsa, ed25519 or secp256k1")
flag.Parse()
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader)
var priv ic.PrivKey
var err error
switch *keyType {
case "rsa":
fmt.Printf("Generated new peer with an RSA key.")
priv, _, err = ic.GenerateRSAKeyPair(2048, rand.Reader)
case "ecdsa":
fmt.Printf("Generated new peer with an ECDSA key.")
priv, _, err = ic.GenerateECDSAKeyPair(rand.Reader)
case "ed25519":
fmt.Printf("Generated new peer with an Ed25519 key.")
priv, _, err = ic.GenerateEd25519Key(rand.Reader)
case "secp256k1":
fmt.Printf("Generated new peer with an Secp256k1 key.")
priv, _, err = ic.GenerateSecp256k1Key(rand.Reader)
default:
return fmt.Errorf("unknown key type: %s", *keyType)
}
if err != nil {
return err
}
id, err := peer.IDFromPrivateKey(priv)
if err != nil {
return err
}
fmt.Printf("Generated new peer with an ECDSA key. Peer ID: %s\n", id.Pretty())
fmt.Printf(" Peer ID: %s\n", id.Pretty())
tp, err := libp2ptls.New(priv)
if err != nil {
return err
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment