docker-ipfs-keygen/main.go

110 lines
2.2 KiB
Go
Raw Normal View History

2016-01-22 12:22:54 +00:00
package main
import (
// 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
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")
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 != "" {
if err := readKey(key, typ); err != nil {
2022-06-04 12:07:00 +00:00
fmt.Fprintln(os.Stderr, err)
}
return
}
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
}
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)
var unmarshalPrivateKeyFunc func(data []byte) (crp.PrivKey, error)
switch strings.ToLower(*typ) {
case "rsa":
unmarshalPrivateKeyFunc = crp.UnmarshalRsaPrivateKey
case "ed25519":
unmarshalPrivateKeyFunc = crp.UnmarshalEd25519PrivateKey
default:
unmarshalPrivateKeyFunc = crp.UnmarshalPrivateKey
}
prvKey, err := unmarshalPrivateKeyFunc(data)
2022-06-04 12:07:00 +00:00
if err != nil {
return err
}
id, err := peer.IDFromPrivateKey(prvKey)
2022-06-04 12:07:00 +00:00
if err != nil {
return err
}
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 {
var keyType int
2016-12-13 14:07:39 +00:00
switch strings.ToLower(*typ) {
case "rsa":
keyType = crp.RSA
2016-12-13 14:07:39 +00:00
case "ed25519":
keyType = crp.Ed25519
case "secp256k1":
keyType = crp.Secp256k1
case "ecdsa":
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
}
fmt.Fprintf(os.Stderr, "Generating a %d-bit %s key...\n", *size, *typ)
2022-06-04 12:07:00 +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
}
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
}
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 {
return err
2016-01-22 12:22:54 +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
}