diff options
author | fuero <fuero@users.noreply.github.com> | 2017-06-05 23:28:13 +0200 |
---|---|---|
committer | Niall Sheridan <nsheridan@gmail.com> | 2017-06-05 22:28:13 +0100 |
commit | 321e26fae746e661d713cedfb6642609e680cafe (patch) | |
tree | 4be9496c75aabc54325aac1b8b511223e986b8f7 /client/keys.go | |
parent | 8ee3c6473f3e2373303b9cb16ab5f059f9e6369e (diff) |
Saving private keys (#61)
* enables saving private keys
* renames public_file_prefix to key_file_prefix and updates its docs to better reflect the changes
Diffstat (limited to 'client/keys.go')
-rw-r--r-- | client/keys.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/client/keys.go b/client/keys.go index 73983a8..b488ea2 100644 --- a/client/keys.go +++ b/client/keys.go @@ -6,12 +6,16 @@ import ( "crypto/elliptic" "crypto/rand" "crypto/rsa" + "crypto/x509" + "encoding/pem" "fmt" "github.com/pkg/errors" "golang.org/x/crypto/ed25519" "golang.org/x/crypto/ssh" + + "github.com/mikesmitty/edkey" ) // Key is a private key. @@ -32,6 +36,24 @@ var defaultOptions = options{ // A KeyOption is used to generate keys of different types and sizes. type KeyOption func(*options) +func pemBlockForKey(priv interface{}) (*pem.Block, error) { + switch k := priv.(type) { + case *rsa.PrivateKey: + return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}, nil + case *ecdsa.PrivateKey: + b, err := x509.MarshalECPrivateKey(k) + if err != nil { + return nil, err + } + return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}, nil + case *ed25519.PrivateKey: + b := edkey.MarshalED25519PrivateKey(*k) + return &pem.Block{Type: "OPENSSH PRIVATE KEY", Bytes: b}, nil + default: + return nil, fmt.Errorf("Unable to create PEM blck from key") + } +} + // KeyType sets the type of key to generate. // Valid types are: "rsa", "ecdsa", "ed25519". // Default is "rsa" |