Go - 如何从公钥生成 SSH 公钥指纹,公钥的类型可能是 [ rsa dsa ssh-rsa ssh-dss ecdsa ] 之一

fis*_*shu 6 go ssh-keys

我只有一个公钥字符串,如何获取公钥指纹?我有一些想法来自https://go-review.googlesource.com/c/crypto/+/32814,但我不知道如何
实现 ssh.PublicKey 接口。

Ken*_*ant 9

您可能想使用 ssh 包中的 ssh.ParseAuthorizedKey 来加载密钥:

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

这会给你一个公钥,你可以调用 ssh.FingerprintLegacyMD5 来获取指纹(假设这里你想要 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 me@myplace.local`)

    // 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)
}
Run Code Online (Sandbox Code Playgroud)

提供了两种指纹功能,不确定您需要哪一种。