diff options
author | Gibheer <gibheer@gmail.com> | 2014-10-27 22:42:52 +0100 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2014-10-27 22:42:52 +0100 |
commit | b78544da1c61a6cfc0d0e1e8e4c4dc94a6a03200 (patch) | |
tree | 447dab7f1fac48cd479eec957004a57a10c5b2a0 | |
parent | 0f9264579e0bf687b48acc362d7e2c5d742e5899 (diff) |
add rsa private keys
-rw-r--r-- | main.go | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -8,6 +8,7 @@ import ( "path/filepath" "crypto/elliptic" "crypto/ecdsa" + "crypto/rsa" "crypto/x509" // "crypto/x509/pkix" "crypto/rand" @@ -73,7 +74,18 @@ func create_private_key() { // generate a rsa private key func create_private_key_rsa(flags CreateFlags) { + if flags.CryptLength < 2048 { + crash_with_help(2, "Length is smaller than 2048!") + } + priv, err := rsa.GenerateKey( rand.Reader, flags.CryptLength) + if err != nil { + fmt.Fprintln(os.Stderr, "Error: ", err) + os.Exit(3) + } + marshal := x509.MarshalPKCS1PrivateKey(priv) + block := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: marshal} + pem.Encode(flags.output_stream, block) } // generate a ecdsa private key @@ -89,14 +101,14 @@ func create_private_key_ecdsa(flags CreateFlags) { priv, err := ecdsa.GenerateKey(curve, rand.Reader) if err != nil { - fmt.Println("Error: ", err) + fmt.Fprintln(os.Stderr, "Error: ", err) os.Exit(3) } - result, err := x509.MarshalECPrivateKey(priv) + marshal, err := x509.MarshalECPrivateKey(priv) if err != nil { crash_with_help(2, fmt.Sprintf("Problems marshalling the private key: %s", err)) } - block := &pem.Block{Type: "ECDSA PRIVATE KEY", Bytes: result} + block := &pem.Block{Type: "ECDSA PRIVATE KEY", Bytes: marshal} pem.Encode(flags.output_stream, block) } |