diff --git a/README.md b/README.md index 8088620..bc28bfe 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,7 @@ $ go get github.com/whyrusleeping/ipfs-key When run, it will write the bytes of the serialized private key to stdout. By default, a 2048 bit RSA key will be generated. In this case the key size can be changed by specifying the `-bitsize` -option. The key type can be changed by specifying the `-type` option (rsa or -ed25519). +option. The key type can be changed by specifying the `-type` option (RSA, Ed25519, Secp256k1 or ECDSA). ``` $ ipfs-key -bitsize=4096 > my-rsa4096.key @@ -41,7 +40,14 @@ ID for generated key: 12D3KooWHM4kLNwS2FzN5GtG5Dfy9h7dLTRs3rtuF9NiR4mjBv3h $ ipfs-key -key my-ed.key Reading key at: my-ed.key Success! -ID for key: 12D3KooWF1TKgiqLMh14za7dWMN5RFRC1WAvgHYioksmdwuhZkzT +ID for ed25519 key: 12D3KooWF1TKgiqLMh14za7dWMN5RFRC1WAvgHYioksmdwuhZkzT +``` +For backward compatibility, to read RSA and Ed25519 keys generated with raw(), specify the `-type rsa` or `-type ed25519` before the `-key` +``` +$ ipfs-key --type rsa -key my-ed.key +Reading key at: my-ed.key +Success! +ID for rsa key: 12D3KooWF1TKgiqLMh14za7dWMN5RFRC1WAvgHYioksmdwuhZkzT ``` ## Contribute diff --git a/main.go b/main.go index 99d933c..aed614f 100644 --- a/main.go +++ b/main.go @@ -13,25 +13,28 @@ import ( func main() { size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate") - typ := flag.String("type", "RSA", "select type of key to generate (RSA or Ed25519)") + typ := flag.String("type", "", "select type of key to generate (RSA, Ed25519, Secp256k1 or ECDSA)") key := flag.String("key", "", "specify the location of the key to decode it's peerID") flag.Parse() if *key != "" { - if err := readKey(key); err != nil { + if err := readKey(key, typ); err != nil { fmt.Fprintln(os.Stderr, err) } return } + if *typ == "" { + *typ = "RSA" + } if err := genKey(typ, size); err != nil { fmt.Fprintln(os.Stderr, err) } return } -func readKey(keyLoc *string) error { +func readKey(keyLoc *string, typ *string) error { data, err := ioutil.ReadFile(*keyLoc) if err != nil { return err @@ -39,7 +42,19 @@ func readKey(keyLoc *string) error { fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc) - prvk, err := crp.UnmarshalPrivateKey(data) + 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) if err != nil { return err } @@ -49,7 +64,7 @@ func readKey(keyLoc *string) error { return err } - _, err = fmt.Fprintf(os.Stderr, "Success!\nID for key: %s\n", id.Pretty()) + _, err = fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvk.Type().String(), id.Pretty()) return err } @@ -60,6 +75,10 @@ func genKey(typ *string, size *int) error { atyp = crp.RSA case "ed25519": atyp = crp.Ed25519 + case "secp256k1": + atyp = crp.Secp256k1 + case "ecdsa": + atyp = crp.ECDSA default: return fmt.Errorf("unrecognized key type: %s", *typ) }