gpt4 book ai didi

go - 加密/ssh ParsePublicKey "short read"错误

转载 作者:IT王子 更新时间:2023-10-29 02:02:26 29 4
gpt4 key购买 nike

在我正在开发的程序中,我需要一种在开发过程中将公钥添加到 authorized_keys 文件中的方法,因此我使用命令行参数来执行此操作。

我省略了大部分代码,但如果您想查看所有代码,here is the repository ,问题行位于第 20 行的 main.go 中。

b, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatalf("Fatal error trying to read new public key file: %s", err)
}

newAuthorizedKey, err := ssh.ParsePublicKey(b)
if err != nil {
log.Fatalf("Fatal error trying to parse new public key: %s", err)
}

“短读”错误来自 ssh.ParsePublicKey 函数。我传入的命令行参数是要添加到该程序的 authorized_keys 文件的公钥位置(例如 ~/.ssh/id_rsa.pub)。我已确保文件已正确传递到程序中。

我查看了源代码,希望能调试这个“short read”错误,但我无法弄清楚发生了什么。 crypto/sshParsePublicKey 函数的源代码位置位于 here ,以及 parseString 函数的源代码位置,ParsePublicKey 函数用来生成“短读”错误,位于 here ,也在 crypto/ssh 中。

最佳答案

我认为对这个问题的一些评论导致了这个问题,但是函数 ssh.ParseAuthorizedKey([]byte) 能够读取解释文件 ~/.ssh/id_rsa.pub.

https://godoc.org/golang.org/x/crypto/ssh#ParseAuthorizedKey

你的例子应该像这样工作:

b, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatalf("Fatal error trying to read new public key file: %s", err)
}

newAuthorizedKey, _, _, _, err := ssh.ParseAuthorizedKey(b)
if err != nil {
log.Fatalf("Fatal error trying to parse new public key: %s", err)
}

关于go - 加密/ssh ParsePublicKey "short read"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48016118/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com