Documentation
¶
Overview ¶
Package ssh provides SSH key utilities for CLI applications.
This package includes:
- SSH key discovery (find default key, list all keys)
- Public key parsing and fingerprint computation
- SSH agent connection and signing
- Direct key file signing
Finding SSH Keys ¶
Find the default SSH key:
info, err := ssh.FindDefaultKey()
if err != nil {
log.Fatal(err)
}
fmt.Println(info.Fingerprint) // SHA256:...
List all SSH keys in ~/.ssh:
keys, err := ssh.ListLocalKeys()
for _, key := range keys {
fmt.Printf("%s: %s\n", key.KeyType, key.Fingerprint)
}
SSH Agent Signing ¶
Sign a challenge using the SSH agent:
agent, err := ssh.GetAgent()
if err != nil {
log.Fatal(err)
}
sig, err := ssh.SignWithAgent(agent, fingerprint, challengeBytes)
Direct Key Signing ¶
Sign using a private key file (unencrypted keys only):
sig, err := ssh.SignWithKeyFile(keyPath, challengeBytes)
Custom Configuration ¶
Use Config for custom SSH directory or key preferences:
cfg := ssh.Config{
SSHDir: "/custom/path/.ssh",
PreferredKeys: []string{"id_ed25519.pub", "id_ecdsa.pub"},
}
info, err := ssh.FindDefaultKeyWithConfig(cfg)
Index ¶
- Variables
- func ComputeFingerprint(keyBlob []byte) string
- func FindAgentKeyByFingerprint(ag agent.Agent, fingerprint string) (*agent.Key, error)
- func ListAgentKeys(ag agent.Agent) ([]*agent.Key, error)
- func SignChallengeWithAgent(ag agent.ExtendedAgent, fingerprint, challenge string) (string, error)
- func SignChallengeWithKeyFile(keyPath, challenge string) (string, error)
- func SignWithAgent(ag agent.ExtendedAgent, fingerprint string, data []byte) (string, error)
- func SignWithKeyFile(keyPath string, data []byte) (string, error)
- type AgentConnection
- type Config
- type KeyInfo
- func FindDefaultKey() (*KeyInfo, error)
- func FindDefaultKeyWithConfig(cfg Config) (*KeyInfo, error)
- func ListLocalKeys() ([]*KeyInfo, error)
- func ListLocalKeysWithConfig(cfg Config) ([]*KeyInfo, error)
- func ParsePublicKey(path, keyData string) (*KeyInfo, error)
- func ReadPublicKey(path string) (*KeyInfo, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoSSHAgent is returned when the SSH agent is not available. ErrNoSSHAgent = errors.New("ssh-agent not available") // ErrNoSSHKeys is returned when no SSH keys are found. ErrNoSSHKeys = errors.New("no SSH keys found") // ErrKeyNotFound is returned when a specific key is not found in the agent. ErrKeyNotFound = errors.New("SSH key not found in agent") // ErrInvalidKeyFormat is returned when a public key file has invalid format. ErrInvalidKeyFormat = errors.New("invalid SSH public key format") )
SSH key errors.
var DefaultPreferredKeys = []string{
"id_ed25519.pub",
"id_ecdsa.pub",
"id_rsa.pub",
}
DefaultPreferredKeys is the default key preference order.
Functions ¶
func ComputeFingerprint ¶
ComputeFingerprint computes the SHA256 fingerprint of a key blob.
func FindAgentKeyByFingerprint ¶
FindAgentKeyByFingerprint finds a key in the agent by its fingerprint.
func ListAgentKeys ¶
ListAgentKeys lists all keys currently in the SSH agent.
func SignChallengeWithAgent ¶
func SignChallengeWithAgent(ag agent.ExtendedAgent, fingerprint, challenge string) (string, error)
SignChallengeWithAgent signs a base64-encoded challenge using the SSH agent. This is a convenience wrapper for challenge-response authentication.
func SignChallengeWithKeyFile ¶
SignChallengeWithKeyFile signs a base64-encoded challenge using a key file.
func SignWithAgent ¶
SignWithAgent signs data using the SSH agent. The fingerprint is used to find the correct key in the agent. Returns the signature encoded as base64.
Types ¶
type AgentConnection ¶
type AgentConnection struct {
agent.ExtendedAgent
// contains filtered or unexported fields
}
AgentConnection wraps an SSH agent with its underlying connection for proper resource cleanup.
func GetAgent ¶
func GetAgent() (*AgentConnection, error)
GetAgent connects to the SSH agent via SSH_AUTH_SOCK. The returned AgentConnection should be closed when done to avoid resource leaks.
func (*AgentConnection) Close ¶
func (a *AgentConnection) Close() error
Close closes the underlying connection to the SSH agent.
type Config ¶
type Config struct {
// SSHDir is the SSH directory path.
// Defaults to ~/.ssh if empty.
SSHDir string
// PreferredKeys is the preference order for key types.
// Defaults to ed25519, ecdsa, rsa if empty.
PreferredKeys []string
}
Config holds configuration for SSH key operations.
type KeyInfo ¶
type KeyInfo struct {
// Path is the path to the public key file.
Path string
// PublicKey is the full public key in authorized_keys format.
PublicKey string
// KeyType is the key algorithm (e.g., "ssh-ed25519", "ssh-rsa").
KeyType string
// Fingerprint is the SHA256 fingerprint of the key.
Fingerprint string
// Comment is the optional key comment.
Comment string
}
KeyInfo holds information about an SSH key.
func FindDefaultKey ¶
FindDefaultKey finds the default SSH key using default configuration.
func FindDefaultKeyWithConfig ¶
FindDefaultKeyWithConfig finds the default SSH key using custom configuration.
func ListLocalKeys ¶
ListLocalKeys lists all SSH public keys in the SSH directory.
func ListLocalKeysWithConfig ¶
ListLocalKeysWithConfig lists all SSH public keys using custom configuration.
func ParsePublicKey ¶
ParsePublicKey parses an SSH public key string.
func ReadPublicKey ¶
ReadPublicKey reads and parses an SSH public key file.