Skip to content

Commit

Permalink
feat: return ssh.Signer
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Apr 11, 2023
1 parent 3dbbf0c commit 8d80e76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
11 changes: 7 additions & 4 deletions keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,15 @@ func (s *SSHKeyPair) PrivateKey() crypto.PrivateKey {
}
}

// Signer returns an ssh.Signer for the key pair.
func (s *SSHKeyPair) Signer() ssh.Signer {
sk, _ := ssh.NewSignerFromKey(s.PrivateKey())
return sk
}

// PublicKey returns the ssh.PublicKey for the key pair.
func (s *SSHKeyPair) PublicKey() ssh.PublicKey {
p, err := ssh.NewPublicKey(s.cryptoPublicKey())
if err != nil {
return nil
}
p, _ := ssh.NewPublicKey(s.cryptoPublicKey())
return p
}

Expand Down
33 changes: 33 additions & 0 deletions keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keygen
import (
"bytes"
"crypto/elliptic"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -19,6 +20,38 @@ func TestNewSSHKeyPair(t *testing.T) {
}
}

func TestNilSSHKeyPair(t *testing.T) {
for _, kt := range []KeyType{RSA, Ed25519, ECDSA} {
t.Run(fmt.Sprintf("test nil key pair for %s", kt), func(t *testing.T) {
kp, err := New("", WithKeyType(kt))
if err != nil {
t.Errorf("error creating SSH key pair: %v", err)
}
if kp == nil {
t.Error("expected key pair to be non-nil")
}
if kp.PrivateKey() == nil {
t.Error("expected private key to be non-nil")
}
if kp.PublicKey() == nil {
t.Error("expected public key to be non-nil")
}
if kp.RawPrivateKey() == nil {
t.Error("expected raw private key to be non-nil")
}
if kp.RawProtectedPrivateKey() == nil {
t.Error("expected raw protected private key to be non-nil")
}
if kp.AuthorizedKey() == "" {
t.Error("expected authorized key to be non-nil")
}
if kp.Signer() == nil {
t.Error("expected signer to be non-nil")
}
})
}
}

func TestGenerateEd25519Keys(t *testing.T) {
// Create temp directory for keys
dir := t.TempDir()
Expand Down

0 comments on commit 8d80e76

Please sign in to comment.