mirror of
https://github.com/hanzo-ds/native.git
synced 2026-10-30 20:00:34 +00:00
Module path, package names, and identifiers de-branded to native/proto/compress/ pool/tracing. Server wire-protocol strings preserved. Vendored codec surface used by hanzo-ds/go (proto + compress).
31 lines
762 B
Go
31 lines
762 B
Go
package ssh
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// LoadPrivateKeyFromFile loads an SSH private key from a file and returns a signer.
|
|
// Supports passphrase-protected keys for RSA, ECDSA, and Ed25519 key types.
|
|
func LoadPrivateKeyFromFile(filename, passphrase string) (ssh.Signer, error) {
|
|
if filename == "" {
|
|
return nil, fmt.Errorf("filename cannot be empty")
|
|
}
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
return nil, fmt.Errorf("failed to access file: %w", err)
|
|
}
|
|
|
|
data, err := os.ReadFile(filename) //nolint:gosec
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read SSH key file: %w", err)
|
|
}
|
|
|
|
if passphrase != "" {
|
|
return ssh.ParsePrivateKeyWithPassphrase(data, []byte(passphrase))
|
|
}
|
|
return ssh.ParsePrivateKey(data)
|
|
}
|