ssh.go

 1package sshutils
 2
 3import (
 4	"bytes"
 5
 6	"github.com/charmbracelet/ssh"
 7	gossh "golang.org/x/crypto/ssh"
 8)
 9
10// ParseAuthorizedKey parses an authorized key string into a public key.
11func ParseAuthorizedKey(ak string) (gossh.PublicKey, string, error) {
12	pk, c, _, _, err := gossh.ParseAuthorizedKey([]byte(ak))
13	return pk, c, err
14}
15
16// MarshalAuthorizedKey marshals a public key into an authorized key string.
17//
18// This is the inverse of ParseAuthorizedKey.
19// This function is a copy of ssh.MarshalAuthorizedKey, but without the trailing newline.
20// It returns an empty string if pk is nil.
21func MarshalAuthorizedKey(pk gossh.PublicKey) string {
22	if pk == nil {
23		return ""
24	}
25	return string(bytes.TrimSuffix(gossh.MarshalAuthorizedKey(pk), []byte("\n")))
26}
27
28// KeysEqual returns whether the two public keys are equal.
29func KeysEqual(a, b gossh.PublicKey) bool {
30	return ssh.KeysEqual(a, b)
31}
32
33// FingerprintSHA256 returns the fingerprint of a public key.
34// This returns the same value as ssh.FingerprintSHA256.
35func FingerprintSHA256(pk gossh.PublicKey) string {
36	return gossh.FingerprintSHA256(pk)
37}