2016-01-22 12:22:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-13 11:58:21 +00:00
|
|
|
// Update the import path for crypto
|
2016-01-22 12:22:54 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-06-04 12:07:00 +00:00
|
|
|
"io/ioutil"
|
2016-01-22 12:22:54 +00:00
|
|
|
"os"
|
2016-12-13 14:14:21 +00:00
|
|
|
"strings"
|
2016-01-22 12:22:54 +00:00
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
crp "github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
peer "github.com/libp2p/go-libp2p/core/peer"
|
2016-01-22 12:22:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
|
2023-11-13 11:58:21 +00:00
|
|
|
typ := flag.String("type", "", "select the type of key to generate (RSA, Ed25519, Secp256k1, or ECDSA)")
|
|
|
|
key := flag.String("key", "", "specify the location of the key to decode its peer ID")
|
2016-01-22 12:22:54 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-06-04 12:07:00 +00:00
|
|
|
if *key != "" {
|
2022-06-04 12:50:09 +00:00
|
|
|
if err := readKey(key, typ); err != nil {
|
2022-06-04 12:07:00 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-04 12:50:09 +00:00
|
|
|
if *typ == "" {
|
|
|
|
*typ = "RSA"
|
|
|
|
}
|
2022-06-04 12:07:00 +00:00
|
|
|
if err := genKey(typ, size); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-04 12:50:09 +00:00
|
|
|
func readKey(keyLoc *string, typ *string) error {
|
2022-06-04 12:07:00 +00:00
|
|
|
data, err := ioutil.ReadFile(*keyLoc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc)
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
var unmarshalPrivateKeyFunc func(data []byte) (crp.PrivKey, error)
|
2022-06-04 12:50:09 +00:00
|
|
|
switch strings.ToLower(*typ) {
|
|
|
|
case "rsa":
|
2023-11-13 11:58:21 +00:00
|
|
|
unmarshalPrivateKeyFunc = crp.UnmarshalRsaPrivateKey
|
2022-06-04 12:50:09 +00:00
|
|
|
case "ed25519":
|
2023-11-13 11:58:21 +00:00
|
|
|
unmarshalPrivateKeyFunc = crp.UnmarshalEd25519PrivateKey
|
2022-06-04 12:50:09 +00:00
|
|
|
default:
|
2023-11-13 11:58:21 +00:00
|
|
|
unmarshalPrivateKeyFunc = crp.UnmarshalPrivateKey
|
2022-06-04 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
prvKey, err := unmarshalPrivateKeyFunc(data)
|
2022-06-04 12:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
id, err := peer.IDFromPrivateKey(prvKey)
|
2022-06-04 12:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvKey.Type().String(), id)
|
|
|
|
return nil
|
2022-06-04 12:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genKey(typ *string, size *int) error {
|
2023-11-13 11:58:21 +00:00
|
|
|
var keyType int
|
2016-12-13 14:07:39 +00:00
|
|
|
switch strings.ToLower(*typ) {
|
|
|
|
case "rsa":
|
2023-11-13 11:58:21 +00:00
|
|
|
keyType = crp.RSA
|
2016-12-13 14:07:39 +00:00
|
|
|
case "ed25519":
|
2023-11-13 11:58:21 +00:00
|
|
|
keyType = crp.Ed25519
|
2022-06-04 12:50:09 +00:00
|
|
|
case "secp256k1":
|
2023-11-13 11:58:21 +00:00
|
|
|
keyType = crp.Secp256k1
|
2022-06-04 12:50:09 +00:00
|
|
|
case "ecdsa":
|
2023-11-13 11:58:21 +00:00
|
|
|
keyType = crp.ECDSA
|
2016-01-22 12:22:54 +00:00
|
|
|
default:
|
2022-06-04 12:07:00 +00:00
|
|
|
return fmt.Errorf("unrecognized key type: %s", *typ)
|
2016-01-22 12:22:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Generating a %d-bit %s key...\n", *size, *typ)
|
2022-06-04 12:07:00 +00:00
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
prvKey, pubKey, err := crp.GenerateKeyPair(keyType, *size)
|
2016-01-22 12:22:54 +00:00
|
|
|
if err != nil {
|
2022-06-04 12:07:00 +00:00
|
|
|
return err
|
2016-01-22 12:22:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
id, err := peer.IDFromPublicKey(pubKey)
|
2016-01-22 12:22:54 +00:00
|
|
|
if err != nil {
|
2022-06-04 12:07:00 +00:00
|
|
|
return err
|
2016-01-22 12:22:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
data, err := crp.MarshalPrivateKey(prvKey)
|
2022-06-04 12:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-22 12:22:54 +00:00
|
|
|
|
2022-06-04 12:07:00 +00:00
|
|
|
_, err = os.Stdout.Write(data)
|
2016-01-22 12:22:54 +00:00
|
|
|
if err != nil {
|
2023-11-13 11:58:21 +00:00
|
|
|
return err
|
2016-01-22 12:22:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:58:21 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Success!\nID for the generated key: %s\n", id)
|
|
|
|
return nil
|
2016-01-22 12:22:54 +00:00
|
|
|
}
|