Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go - How to Generate an SSH PublicKey Fingerprint from PublicKey, the PublicKey's type maybe is one of [ rsa dsa ssh-rsa ssh-dss ecdsa ]

Tags:

go

ssh-keys

I only have a PublicKey string, How do I get the PublicKey Fingerprint? I have got some idea form https://go-review.googlesource.com/c/crypto/+/32814, but I do not know how to
implement ssh.PublicKey interface.

like image 514
fishu Avatar asked Sep 26 '17 12:09

fishu


1 Answers

You probably want to use ssh.ParseAuthorizedKey from the ssh package to load the key:

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

That will give you a public key which you can call ssh.FingerprintLegacyMD5 on in order to get the fingerprint (assuming here you want the md5).

https://godoc.org/golang.org/x/crypto/ssh#FingerprintLegacyMD5 https://godoc.org/golang.org/x/crypto/ssh#FingerprintSHA256

func main() {
    // Read a key from a file in authorized keys file line format
    // This could be an rsa.pub file or a line from authorized_keys
    pubKeyBytes := []byte(`ssh-rsa AAAABMYKEY...ABC [email protected]`)

    // Parse the key, other info ignored
    pk, _, _, _, err := ssh.ParseAuthorizedKey(pubKeyBytes)
    if err != nil {
        panic(err)
    }

    // Get the fingerprint
    f := ssh.FingerprintLegacyMD5(pk)

    // Print the fingerprint
    fmt.Printf("%s\n", f)
}

There are two fingerprint functions provided, not sure which one you need.

like image 199
Kenny Grant Avatar answered Nov 10 '22 04:11

Kenny Grant