add a command line flag to set the key type

This commit is contained in:
Marten Seemann 2019-02-20 18:10:23 +08:00
parent 7d129c2a6f
commit e4b8bb72f3
2 changed files with 45 additions and 7 deletions

View File

@ -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()
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
}
peerID, err := peer.IDB58Decode(*peerIDString)
if err != nil {
return err
}
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader)
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

View File

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