20

我正在尝试了解获取 OpenSSH 公钥的步骤,如下所示:

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

然后将其转换为标准指纹,如下所示:

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

我试图深入研究 OpenSSH 源代码来理解这一点,但这超出了我的想象。我的第一个猜测是对关键文本做一个简单的 MD5,但结果与上面的输出不匹配。

4

2 回答 2

46

它是 base64 编码密钥的 MD5 和:

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]))
于 2011-07-13T17:19:29.343 回答
5

https://github.com/ojarva/sshpubkeys

pip install sshpubkeys

用法:

import sshpubkeys
key = sshpubkeys.SSHKey("ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqmEmDTNBC6O8H" +
    "GCdu0MZ9zLCivDsYSttrrmlq87/YsEBpvwUTiF3UEQuFLaq5Gm+dtgxJewg/UwsZrDFxz" +
    "pQhCHB6VmqrbKN2hEIkk/HJvCnAmR1ehXv8n2BWw3Jlw7Z+VgWwXAH50f2HWYqTaE4qP4" +
    "Dxc4RlElxgNmlDPGXw/dYBvChYBG/RvIiTz1L+pYzPD4JR54IMmTOwjcGIJl7nk1VjKvl" +
    "3D8Wgp6qejv4MfZ7Htdc99SUKcKWAeHYsjPXosSk3GlwKiS/sZi51Yca394GE7T4hZu6H" +
    "TaXeZoD8+IZ7AijYn89H7EPjuu0iCAa/cjVzBsFHGszQYG+U5KfIw== user@host")
print(key.bits)   # 2048
print(key.hash()) # '49:d3:cb:f6:00:d2:93:43:a6:27:07:ca:12:fd:5d:98'
于 2014-12-18T14:15:43.677 回答