Go - 如何从PublicKey生成一个SSH公共密钥指纹,PublicKey的类型可能是[rsa dsa ssh-rsa ssh-dss ecdsa] [英] 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 ]

查看:1095
本文介绍了Go - 如何从PublicKey生成一个SSH公共密钥指纹,PublicKey的类型可能是[rsa dsa ssh-rsa ssh-dss ecdsa]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

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

这会给你一个公钥,你可以调用ssh.FingerprintLegacyMD5来获得指纹(假设你想要md5)。



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

  func main(){
//从授权密钥文件中读取密钥文件行格式
//这可以是rsa.pub文件或来自authorized_keys的行
pubKeyBytes := [] byte(`ssh-rsa AAAABMYKEY ... ABC me @ myplace.local`)

//解析键,忽略其他信息
pk,_,_,_ ,err:= ssh.ParseAuthorizedKey(pubKeyBytes)
if err!= nil {
panic(err)
}

//获取指纹
f := ssh.FingerprintLegacyMD5(pk)

//打印指纹
fmt.Printf(%s \ n,f)
}
code>

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

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.

解决方案

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

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

这篇关于Go - 如何从PublicKey生成一个SSH公共密钥指纹,PublicKey的类型可能是[rsa dsa ssh-rsa ssh-dss ecdsa]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆