From b78544da1c61a6cfc0d0e1e8e4c4dc94a6a03200 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Mon, 27 Oct 2014 22:42:52 +0100 Subject: add rsa private keys --- main.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 1b1c0d9..1b7a8f4 100644 --- a/main.go +++ b/main.go @@ -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) } -- cgit v1.2.3-70-g09d2