add support for reading raw keys and support new key types
This commit is contained in:
parent
23603526c1
commit
7a9269b304
12
README.md
12
README.md
|
@ -22,8 +22,7 @@ $ go get github.com/whyrusleeping/ipfs-key
|
||||||
When run, it will write the bytes of
|
When run, it will write the bytes of
|
||||||
the serialized private key to stdout. By default, a 2048 bit RSA key will be
|
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`
|
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
|
option. The key type can be changed by specifying the `-type` option (RSA, Ed25519, Secp256k1 or ECDSA).
|
||||||
ed25519).
|
|
||||||
|
|
||||||
```
|
```
|
||||||
$ ipfs-key -bitsize=4096 > my-rsa4096.key
|
$ ipfs-key -bitsize=4096 > my-rsa4096.key
|
||||||
|
@ -41,7 +40,14 @@ ID for generated key: 12D3KooWHM4kLNwS2FzN5GtG5Dfy9h7dLTRs3rtuF9NiR4mjBv3h
|
||||||
$ ipfs-key -key my-ed.key
|
$ ipfs-key -key my-ed.key
|
||||||
Reading key at: my-ed.key
|
Reading key at: my-ed.key
|
||||||
Success!
|
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
|
## Contribute
|
||||||
|
|
29
main.go
29
main.go
|
@ -13,25 +13,28 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
|
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")
|
key := flag.String("key", "", "specify the location of the key to decode it's peerID")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *key != "" {
|
if *key != "" {
|
||||||
if err := readKey(key); err != nil {
|
if err := readKey(key, typ); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *typ == "" {
|
||||||
|
*typ = "RSA"
|
||||||
|
}
|
||||||
if err := genKey(typ, size); err != nil {
|
if err := genKey(typ, size); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func readKey(keyLoc *string) error {
|
func readKey(keyLoc *string, typ *string) error {
|
||||||
data, err := ioutil.ReadFile(*keyLoc)
|
data, err := ioutil.ReadFile(*keyLoc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -39,7 +42,19 @@ func readKey(keyLoc *string) error {
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -49,7 +64,7 @@ func readKey(keyLoc *string) error {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,6 +75,10 @@ func genKey(typ *string, size *int) error {
|
||||||
atyp = crp.RSA
|
atyp = crp.RSA
|
||||||
case "ed25519":
|
case "ed25519":
|
||||||
atyp = crp.Ed25519
|
atyp = crp.Ed25519
|
||||||
|
case "secp256k1":
|
||||||
|
atyp = crp.Secp256k1
|
||||||
|
case "ecdsa":
|
||||||
|
atyp = crp.ECDSA
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unrecognized key type: %s", *typ)
|
return fmt.Errorf("unrecognized key type: %s", *typ)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user