commit 8e18153364cfb666a2bb23e1c147374054adc4a8 Author: Jeromy Date: Fri Jan 22 04:22:54 2016 -0800 initial implementation diff --git a/README.md b/README.md new file mode 100644 index 0000000..fca20e5 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/main.go b/main.go new file mode 100644 index 0000000..af7da2e --- /dev/null +++ b/main.go @@ -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) +}