package main import ( "io/ioutil" "log" "golang.org/x/crypto/ssh" ) func main() { pubKeyBytes, err := ioutil.ReadFile("public_key.pub") if err != nil { log.Fatalf("Failed to read public key file: %v", err) } pubKey, err := ssh.ParsePublicKey(pubKeyBytes) if err != nil { log.Fatalf("Failed to parse public key: %v", err) } }
package main import ( "io/ioutil" "log" "golang.org/x/crypto/ssh" ) func main() { pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZGTVP...")) if err != nil { log.Fatalf("Failed to parse public key: %v", err) } pubKeyBytes := ssh.MarshalAuthorizedKey(pubKey) err = ioutil.WriteFile("public_key.pub", pubKeyBytes, 0644) if err != nil { log.Fatalf("Failed to write public key to file: %v", err) } }In this example, we use the ParseAuthorizedKey function from the ssh package to parse an SSH public key from a string and then use the MarshalAuthorizedKey function to convert it into a byte slice that can be written to a file. Package Library: golang.org.x.crypto.ssh