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

标签 go ssh-keys

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

最佳答案

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

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

提供了两种指纹功能,不知道你需要哪一种。

关于Go - 如何从 PublicKey 生成 SSH PublicKey 指纹,PublicKey 的类型可能是 [ rsa dsa ssh-rsa ssh-dss ecdsa ] 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426718/

相关文章:

go - 从我的边缘节点推送 kafka 消息的最佳方式是什么?

ssh - 无法ssh到服务器

struct - 从结构定义中定义 Go 方法有什么好处?

json - 使用接口(interface)与 Golang 映射和结构以及 JSON

git - ssh key github 添加后仍询问 user/pw

bash - 在容器内运行 shell 脚本

linux - Linux 上的 SSH : Disabling host key checking for hosts on local subnet (known_hosts)

ssh 仍然要求输入密码

Goland显示os.Remove()无法解决?

golang gofmt 包重写通配符