keygen

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 15 Imported by: 16

README

Keygen

Latest Release GoDoc Build Status Go ReportCard

An SSH key pair generator with password protected keys support. Supports generating RSA, ECDSA, and Ed25519 keys.

Example

kp, err := keygen.New("awesome", keygen.WithPassphrase("awesome_secret"),
	keygen.WithKeyType(keygen.Ed25519))
if err != nil {
	log.Fatalf("error creating SSH key pair: %v", err)
}
fmt.Printf("Your authorized key: %s\n", kp.AuthorizedKey())

Feedback

We’d love to hear your thoughts on this project. Feel free to drop us a note!

License

MIT


Part of Charm.

The Charm logo

Charm热爱开源 • Charm loves open source

Documentation

Overview

Package keygen handles the creation of new SSH key pairs.

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingSSHKeys = errors.New("missing one or more keys; did something happen to them after they were generated?")

ErrMissingSSHKeys indicates we're missing some keys that we expected to have after generating. This should be an extreme edge case.

Functions

This section is empty.

Types

type ErrUnsupportedKeyType added in v0.3.0

type ErrUnsupportedKeyType struct {
	// contains filtered or unexported fields
}

ErrUnsupportedKeyType indicates an unsupported key type.

func (ErrUnsupportedKeyType) Error added in v0.3.0

func (e ErrUnsupportedKeyType) Error() string

Error implements the error interface for ErrUnsupportedKeyType.

type FilesystemErr

type FilesystemErr struct {
	Err error
}

FilesystemErr is used to signal there was a problem creating keys at the filesystem-level. For example, when we're unable to create a directory to store new SSH keys in.

func (FilesystemErr) Error

func (e FilesystemErr) Error() string

Error returns a human-readable string for the error. It implements the error interface.

func (FilesystemErr) Unwrap

func (e FilesystemErr) Unwrap() error

Unwrap returns the underlying error.

type KeyPair added in v0.5.0

type KeyPair struct {
	// contains filtered or unexported fields
}

KeyPair holds a pair of SSH keys and associated methods.

func New added in v0.1.1

func New(path string, opts ...Option) (*KeyPair, error)

New generates a KeyPair, which contains a pair of SSH keys.

If the key pair already exists, it will be loaded from disk, otherwise, a new SSH key pair is generated. If no key type is specified, Ed25519 will be used.

func (*KeyPair) AuthorizedKey added in v0.5.0

func (s *KeyPair) AuthorizedKey() string

AuthorizedKey returns the SSH public key (RFC 4253) in OpenSSH authorized_keys format. The returned string is trimmed of sshd options and comments.

func (*KeyPair) CryptoPublicKey added in v0.5.0

func (s *KeyPair) CryptoPublicKey() crypto.PublicKey

CryptoPublicKey returns the crypto.PublicKey of the SSH key pair.

func (*KeyPair) KeyPairExists added in v0.5.0

func (s *KeyPair) KeyPairExists() bool

KeyPairExists checks if the SSH key pair exists on disk.

func (*KeyPair) PrivateKey added in v0.5.0

func (s *KeyPair) PrivateKey() crypto.PrivateKey

PrivateKey returns the unencrypted crypto.PrivateKey.

func (*KeyPair) PublicKey added in v0.5.0

func (s *KeyPair) PublicKey() ssh.PublicKey

PublicKey returns the ssh.PublicKey for the key pair.

func (*KeyPair) RawAuthorizedKey added in v0.5.0

func (s *KeyPair) RawAuthorizedKey() []byte

RawAuthorizedKey returns the underlying SSH public key (RFC 4253) in OpenSSH authorized_keys format.

func (*KeyPair) RawPrivateKey added in v0.5.0

func (s *KeyPair) RawPrivateKey() []byte

RawPrivateKey returns the raw unencrypted private key bytes in PEM format.

func (*KeyPair) RawProtectedPrivateKey added in v0.5.0

func (s *KeyPair) RawProtectedPrivateKey() []byte

RawProtectedPrivateKey returns the raw password protected private key bytes in PEM format.

func (*KeyPair) Signer added in v0.5.0

func (s *KeyPair) Signer() ssh.Signer

Signer returns an ssh.Signer for the key pair.

func (*KeyPair) WriteKeys added in v0.5.0

func (s *KeyPair) WriteKeys() error

WriteKeys writes the SSH key pair to disk.

type KeyType added in v0.1.2

type KeyType string

KeyType represents a type of SSH key.

const (
	RSA     KeyType = "rsa"
	Ed25519 KeyType = "ed25519"
	ECDSA   KeyType = "ecdsa"
)

Supported key types.

func (KeyType) String added in v0.4.0

func (k KeyType) String() string

String implements the Stringer interface for KeyType.

type Option added in v0.4.0

type Option func(*KeyPair)

Option is a functional option for KeyPair.

func WithBitSize added in v0.4.0

func WithBitSize(bits int) Option

WithBitSize sets the key size for the RSA key pair. This option is ignored for other key types.

func WithEllipticCurve added in v0.4.0

func WithEllipticCurve(curve elliptic.Curve) Option

WithEllipticCurve sets the elliptic curve for the ECDSA key pair. Supported curves are P-256, P-384, and P-521. The default curve is P-384. This option is ignored for other key types.

func WithKeyType added in v0.4.0

func WithKeyType(keyType KeyType) Option

WithKeyType sets the key type for the key pair. Available key types are RSA, Ed25519, and ECDSA.

func WithPassphrase added in v0.4.0

func WithPassphrase(passphrase string) Option

WithPassphrase sets the passphrase for the private key.

func WithWrite added in v0.4.0

func WithWrite() Option

WithWrite writes the key pair to disk if it doesn't exist.

type SSHKeyPair

type SSHKeyPair = KeyPair

SSHKeyPair holds a pair of SSH keys and associated methods. Deprecated: Use KeyPair instead.

type SSHKeysAlreadyExistErr

type SSHKeysAlreadyExistErr struct {
	Path string
}

SSHKeysAlreadyExistErr indicates that files already exist at the location at which we're attempting to create SSH keys.

func (SSHKeysAlreadyExistErr) Error

func (e SSHKeysAlreadyExistErr) Error() string

Error returns the a human-readable error message for SSHKeysAlreadyExistErr. It satisfies the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL