Skip to content

Commit

Permalink
Add PrivateKeyFileWithPassphrase from elliotchance#11
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesGDiaz committed Jan 11, 2024
1 parent d87a21f commit fd7e608
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion private_key_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"golang.org/x/crypto/ssh"
)

func PrivateKeyFile(file string) ssh.AuthMethod {
func PrivateKeyFile(file string, pass ...string) ssh.AuthMethod {
buffer, err := os.ReadFile(file)
if err != nil {
return nil
Expand All @@ -19,3 +19,18 @@ func PrivateKeyFile(file string) ssh.AuthMethod {

return ssh.PublicKeys(key)
}

// All credits go to the author fagnercarvalho
func PrivateKeyFileWithPassphrase(file string, passphrase string) ssh.AuthMethod {
buffer, err := os.ReadFile(file)
if err != nil {
return nil
}

key, err := ssh.ParsePrivateKeyWithPassphrase(buffer, []byte(passphrase))
if err != nil {
return nil
}

return ssh.PublicKeys(key)
}

0 comments on commit fd7e608

Please sign in to comment.