CESCO
CESCO

Reputation: 7768

SSH Fingerprint with Golang

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

Upvotes: 1

Views: 3282

Answers (3)

Vít Kotačka
Vít Kotačka

Reputation: 1612

In case that you need a fingerprint of an rsa.PublicKey you can use the ssh.FingerprintLegacyMD5() function (or the ssh.FingerprintSHA256() one).

The only issue is that you have to convert the rsa.PublicKey to an ssh.PublicKey which can be done via the ssh.NewPublicKey() function.

func getFingerprint(key *rsa.PublicKey) string {
    pubKey, _ := ssh.NewPublicKey(key)
    return ssh.FingerprintLegacyMD5(pubKey)
}

Upvotes: 1

Kenny Grant
Kenny Grant

Reputation: 9633

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:

https://stackoverflow.com/a/46430102/1601137

Upvotes: 3

Mr_Pink
Mr_Pink

Reputation: 109432

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

Upvotes: 6

Related Questions