Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse an RSA public key in Go?

Tags:

go

Why do I get the error ssh: short read from the following code:

package main

import (
    "golang.org/x/crypto/ssh"
    "testing"
)

func TestPublicKeyParsing(t *testing.T) {
    key := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDhZgLqiZYDKCWhyi2gUXIRwIPyMSyXZ6yrwsm3PYfIvFB60kVlNgqDpPVhWoH6eRfaQ1y/xbg4nClZmHEDTvLbTQ1ZoQzzjZ7zvM6aQ4nADmKcCYswEuU94axouVjsHNyMLfOkPXuGec0fChwQ2JDh/B9LCiSDxyhCOgHvETXGXsyBMKjn498iPjJ6snzk35dy5wPZRz41g3dLaygF+wYAT791u/JchHQL7OP7RoNgby+RM16SYZs1tgQVkfU//o+AyTarWYLVDpFU6HPPenE4xEXhbgqd7x3wSNPBsMvY8Zjcu3kdHtboJidyMtKeD8ghV/T24kME58TW15T8Eg8R"

    _, err := ssh.ParsePublicKey([]byte(key))
    if err != nil {
        t.Errorf("ERROR! %s", err)
    }
}

Is the key string in the wrong format?

What is the correct format for the public key?

like image 774
codefinger Avatar asked Mar 16 '18 16:03

codefinger


1 Answers

This looks like the authorized_keys format, which you can parse with ssh.ParseAuthorizedKey:

package main

import (
    "golang.org/x/crypto/ssh"
    "testing"
)

func TestPublicKeyParsing(t *testing.T) {
    key := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDhZgLqiZYDKCWhyi2gUXIRwIPyMSyXZ6yrwsm3PYfIvFB60kVlNgqDpPVhWoH6eRfaQ1y/xbg4nClZmHEDTvLbTQ1ZoQzzjZ7zvM6aQ4nADmKcCYswEuU94axouVjsHNyMLfOkPXuGec0fChwQ2JDh/B9LCiSDxyhCOgHvETXGXsyBMKjn498iPjJ6snzk35dy5wPZRz41g3dLaygF+wYAT791u/JchHQL7OP7RoNgby+RM16SYZs1tgQVkfU//o+AyTarWYLVDpFU6HPPenE4xEXhbgqd7x3wSNPBsMvY8Zjcu3kdHtboJidyMtKeD8ghV/T24kME58TW15T8Eg8R"

    parsedKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key))
    if err != nil {
        t.Errorf("ERROR! %s", err)
    }
}
like image 171
Peter Avatar answered Oct 10 '22 22:10

Peter