从Python中的公钥派生SSH指纹

时间:2011-07-13 17:10:22

标签: python openssh

我正在尝试理解采用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,但结果与上面的输出不匹配。

2 个答案:

答案 0 :(得分:39)

它是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]))

答案 1 :(得分:4)

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'