我正在尝试使用 go
解析公钥的ssh
库如下:
var testPublicKey = `ssh-rsa AAAAB3NzaC1yc...DEdfU= pkaramol@MacBookPro`
pubKey, err := ssh.ParsePublicKey(([]byte(testPublicKey)))
if err != nil {
log.Fatal(err)
}
但我收到以下错误:
ssh: short read
我知道 this问题,但它解决了另一个问题(从文件中读取公钥并加载到授权 key 列表)
我的目标是最终得到
*rsa.PublicKey
type用于this功能。
最佳答案
key 的线格式和磁盘格式之间存在差异。从文档:
ParsePublicKey parses an SSH public key formatted for use in the SSH wire protocol according to RFC 4253
因此,您需要将其编码为有线格式。
package main
import (
"log"
"golang.org/x/crypto/ssh"
)
func parse(in []byte) error {
// ParseAuthorizedKeys parses a public key from an authorized_keys file used in OpenSSH
pk, _, _, _, err := ssh.ParseAuthorizedKey(in)
if err != nil {
return err
}
// pk.Marshal() Marshal returns the serialized key data in SSH wire format, with the name prefix
// ssh.ParsePublicKey is used to unmarshal the returned data
res, err := ssh.ParsePublicKey(pk.Marshal())
if err != nil {
return err
}
log.Println(res)
return nil
}
func main() {
key := "ssh-rsa AAAAB3NzaC1yc...DEdfU= pkaramol@MacBookPro"
if err := parse([]byte(key)); err != nil {
log.Println(err)
}
}
关于go - 获取 ssh : short read error when trying to parse a public key in golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62236441/