带有Golang的SSH指纹
问题描述:
How can I get my own ssh fingerprint with Go?
Finger, err := ssh.**
log.Println(Finger)
// 12:f8:7e:78:61:b4:bf:e2:de:24:15:96:4e:d4:72:53
如何使用Go获取我自己的ssh指纹? p>
手指,错误:= ssh。**
log.Println(手指)
// 12:f8:7e:78:61:b4:bf:e2:de:24:15:96:4e:d4:72 :53
code> pre>
div>
答
A key signature is is just a cryptographic hash of the unencoded key data in ssh wire format.
Here is a basic example of parsing an ssh key from the OpenSSH format public key, and getting an MD5 fingerprint:
key, err := ioutil.ReadFile("/path/to/id_rsa.pub")
if err != nil {
log.Fatal(err)
}
parts := strings.Fields(string(key))
if len(parts) < 2 {
log.Fatal("bad key")
}
k, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
log.Fatal(err)
}
fp := md5.Sum([]byte(k))
fmt.Print("MD5:")
for i, b := range fp {
fmt.Printf("%02x", b)
if i < len(fp)-1 {
fmt.Print(":")
}
}
fmt.Println()
答
Another way to do this is to parse the key using the ssh package and then use FingerprintLegacyMD5 in order to generate the fingerprint. See this answer: