Skip to content

Commit

Permalink
Use consts for supported key types (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm authored Sep 13, 2021
1 parent fa8424b commit caa79f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ An SSH key pair generator. Supports generating RSA and Ed25519 keys.
## Example

```go
k, err := NewSSHKeyPair(".ssh", "my_awesome_key", []byte(""), "ed25519")
k, err := NewSSHKeyPair(".ssh", "my_awesome_key", []byte(""), key.Ed25519)
if err != nil {
fmt.Printf("error creating SSH key pair: %v", err)
os.Exit(1)
Expand All @@ -34,4 +34,4 @@ Part of [Charm](https://charm.sh).

<a href="https://charm.sh/"><img alt="the Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>

Charm热爱开源 • Charm loves open source
Charm热爱开源 • Charm loves open source
17 changes: 13 additions & 4 deletions keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import (
"golang.org/x/crypto/ssh"
)

// KeyType represents a type of SSH key.
type KeyType string

// Supported key types.
const (
RSA KeyType = "rsa"
Ed25519 KeyType = "ed25519"
)

const rsaDefaultBits = 4096

// ErrMissingSSHKeys indicates we're missing some keys that we expected to
Expand Down Expand Up @@ -67,7 +76,7 @@ func (s SSHKeyPair) publicKeyPath() string {
}

// New generates an SSHKeyPair, which contains a pair of SSH keys.
func New(path, name string, passphrase []byte, keyType string) (*SSHKeyPair, error) {
func New(path, name string, passphrase []byte, keyType KeyType) (*SSHKeyPair, error) {
var err error
s := &SSHKeyPair{
KeyDir: path,
Expand All @@ -87,9 +96,9 @@ func New(path, name string, passphrase []byte, keyType string) (*SSHKeyPair, err
return s, nil
}
switch keyType {
case "ed25519":
case Ed25519:
err = s.generateEd25519Keys()
case "rsa":
case RSA:
err = s.generateRSAKeys(rsaDefaultBits, passphrase)
default:
return nil, fmt.Errorf("unsupported key type %s", keyType)
Expand All @@ -101,7 +110,7 @@ func New(path, name string, passphrase []byte, keyType string) (*SSHKeyPair, err
}

// New generates an SSHKeyPair and writes it to disk if not exist.
func NewWithWrite(path, name string, passphrase []byte, keyType string) (*SSHKeyPair, error) {
func NewWithWrite(path, name string, passphrase []byte, keyType KeyType) (*SSHKeyPair, error) {
s, err := New(path, name, passphrase, keyType)
if err != nil {
return nil, err
Expand Down

0 comments on commit caa79f4

Please sign in to comment.