initial implementation

This commit is contained in:
Jeromy 2016-01-22 04:22:54 -08:00
commit 8e18153364
2 changed files with 64 additions and 0 deletions

16
README.md Normal file
View File

@ -0,0 +1,16 @@
# ipfs-key
A tool for easily generating ipfs keypairs. When run, it will write the bytes of
the serialized private key to stdout. By default, a 2048 bit RSA key will be
generated. The keysize can be changed by specifying the `-size` option, and the
key type can be changed by specifying the `-type` option (currently only RSA is
implemented).
## Installation
```
$ go get github.com/whyrusleeping/ipfs-key
```
## Usage
```
$ ipfs-key -size=4096 > my.key
```

48
main.go Normal file
View File

@ -0,0 +1,48 @@
package main
import (
"flag"
"fmt"
"os"
ci "github.com/ipfs/go-ipfs/p2p/crypto"
peer "github.com/ipfs/go-ipfs/p2p/peer"
)
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")
flag.Parse()
var atyp int
switch *typ {
case "RSA":
atyp = ci.RSA
default:
fmt.Fprintln(os.Stderr, "unrecognized key type: ", *typ)
os.Exit(1)
}
priv, pub, err := ci.GenerateKeyPair(atyp, *size)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "ID for generated key: %s\n", pid.Pretty())
data, err := priv.Bytes()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Stdout.Write(data)
}