Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use consts for supported key types #1

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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