docker-ipfs-keygen/main.go

111 lines
2.3 KiB
Go
Raw Normal View History

2016-01-22 12:22:54 +00:00
package main
import (
"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
2022-06-04 12:07:00 +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 type of key to generate (RSA, Ed25519, Secp256k1 or ECDSA)")
2022-06-04 12:07:00 +00:00
key := flag.String("key", "", "specify the location of the key to decode it's peerID")
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 unmarshalPrivateKeyFucn func(data []byte) (crp.PrivKey, error)
// rsa and ed25519 unmarshalPrivateKeyFucn are for backward compatibility
// for keys saved with raw(), to read such keys, specify the key type
switch strings.ToLower(*typ) {
case "rsa":
unmarshalPrivateKeyFucn = crp.UnmarshalRsaPrivateKey
case "ed25519":
unmarshalPrivateKeyFucn = crp.UnmarshalEd25519PrivateKey
default:
unmarshalPrivateKeyFucn = crp.UnmarshalPrivateKey
}
prvk, err := unmarshalPrivateKeyFucn(data)
2022-06-04 12:07:00 +00:00
if err != nil {
return err
}
id, err := peer.IDFromPrivateKey(prvk)
if err != nil {
return err
}
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvk.Type().String(), id.Pretty())
2022-06-04 12:07:00 +00:00
return err
}
func genKey(typ *string, size *int) error {
2016-01-22 12:22:54 +00:00
var atyp int
2016-12-13 14:07:39 +00:00
switch strings.ToLower(*typ) {
case "rsa":
2022-06-04 12:07:00 +00:00
atyp = crp.RSA
2016-12-13 14:07:39 +00:00
case "ed25519":
2022-06-04 12:07:00 +00:00
atyp = crp.Ed25519
case "secp256k1":
atyp = crp.Secp256k1
case "ecdsa":
atyp = 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
}
2016-02-05 20:50:07 +00:00
fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ)
2022-06-04 12:07:00 +00:00
priv, pub, err := crp.GenerateKeyPair(atyp, *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
}
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
2022-06-04 12:07:00 +00:00
return err
2016-01-22 12:22:54 +00:00
}
2022-06-04 12:07:00 +00:00
data, err := crp.MarshalPrivateKey(priv)
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 {
2022-06-04 12:07:00 +00:00
return nil
2016-01-22 12:22:54 +00:00
}
2022-06-04 12:07:00 +00:00
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for generated key: %s\n", pid.Pretty())
return err
2016-01-22 12:22:54 +00:00
}