crypto/ssh ParsePublicKey “short read” error

ⅰ亾dé卋堺 提交于 2021-02-07 20:17:59

问题


In a program I am developing I need a way to add public keys into the authorized_keys file during development, so I am using command line arguments to do so.

I have omitted most of the code, but if you would like to view all of the code, here is the repository, with the problem line being located in main.go on line 20.

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)
}

The "short read" error comes from the ssh.ParsePublicKey function. The command line argument that I am passing in is the location of a public key to add to the authorized_keys file of this program (e.g. ~/.ssh/id_rsa.pub). I have ensured that the file is correctly being passed into the program.

I have looked at the source code in hopes of debugging this "short read" error, but I can't figure out what is going on. The location for source code of the ParsePublicKey function in crypto/ssh is located here, and the location of source code of the parseString function, which is what the ParsePublicKey function is using to generate the "short read" error, is located here, also in crypto/ssh.


回答1:


I think some of the comments to the question lead at this, but the function ssh.ParseAuthorizedKey([]byte) is able to read interpret the file at ~/.ssh/id_rsa.pub.

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

Your example should work like this:

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)
}


来源:https://stackoverflow.com/questions/48016118/crypto-ssh-parsepublickey-short-read-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!