Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deriving an SSH Fingerprint from a Public Key in Python

Tags:

I'm trying to understand the steps to take an OpenSSH public key like so:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqmEmDTNBC6O8HGCdu0MZ9zLCivDsYSttrrmlq87/YsEBpvwUTiF3UEQuFLaq5Gm+dtgxJewg/UwsZrDFxzpQhCHB6VmqrbKN2hEIkk/HJvCnAmR1ehXv8n2BWw3Jlw7Z+VgWwXAH50f2HWYqTaE4qP4Dxc4RlElxgNmlDPGXw/dYBvChYBG/RvIiTz1L+pYzPD4JR54IMmTOwjcGIJl7nk1VjKvl3D8Wgp6qejv4MfZ7Htdc99SUKcKWAeHYsjPXosSk3GlwKiS/sZi51Yca394GE7T4hZu6HTaXeZoD8+IZ7AijYn89H7EPjuu0iCAa/cjVzBsFHGszQYG+U5KfIw==

And then to convert it into an standard fingerprint like so:

2048 49:d3:cb:f6:00:d2:93:43:a6:27:07:ca:12:fd:5d:98 id_rsa.pub (RSA)

I have attempted to dive into the OpenSSH source to understand this, but it is over my head. My first guess was to do a simple MD5 on the key text, but the result does not match the above output.

like image 732
Michael Gorsuch Avatar asked Jul 13 '11 17:07

Michael Gorsuch


People also ask

How do I get fingerprints from SSH key?

Use ssh-keygen The -l option lists the fingerprint, and the -f /etc/ssh/ssh_host_rsa_key. pub option gives the location of the public key file of the host.

How do I generate a private SSH key from the public key?

To generate an SSH private/public key pair for your use, you can use the ssh-keygen command-line utility. You can run the ssh-keygen command from the command line to generate an SSH private/public key pair. If you are using Windows, by default you may not have access to the ssh-keygen command.

How are SSH fingerprints generated?

The fingerprint is based on the host's public key, usually based on the /etc/ssh/ssh_host_rsa_key. pub file. Generally it's for easy identification/verification of the host you are connecting to. If the fingerprint changes, the machine you are connecting to has changed their public key.


1 Answers

It is the MD5 sum of the base64-encoded key:

import base64
import hashlib

def lineToFingerprint(line):
    key = base64.b64decode(line.strip().split()[1].encode('ascii'))
    fp_plain = hashlib.md5(key).hexdigest()
    return ':'.join(a+b for a,b in zip(fp_plain[::2], fp_plain[1::2]))
like image 109
phihag Avatar answered Sep 19 '22 16:09

phihag