Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/setup_sshkey/files/ec2-fingerprint.py

raw
1 #!/usr/bin/env python
2 """
3 Reads an OpenSSH Public key and spits out the 'AWS' MD5 sum
4 The equivalent of
5
6 ssh-keygen -f id_rsa.pub -e -m PKCS8 | openssl pkey -pubin -outform DER | openssl md5 -c | cut -f 2 -d ' '
7
8 (but without needing the OpenSSL CLI)
9 """
10
11
12 import hashlib
13 import sys
14
15 from cryptography.hazmat.primitives import serialization
16
17 if len(sys.argv) == 0:
18 ssh_public_key = "id_rsa.pub"
19 else:
20 ssh_public_key = sys.argv[1]
21
22 with open(ssh_public_key, "rb") as key_file:
23 public_key = serialization.load_ssh_public_key(
24 key_file.read(),
25 )
26 pub_der = public_key.public_bytes(
27 encoding=serialization.Encoding.DER,
28 format=serialization.PublicFormat.SubjectPublicKeyInfo,
29 )
30 md5digest = hashlib.md5(pub_der).hexdigest()
31 # Format the md5sum into the normal format
32 pairs = zip(md5digest[::2], md5digest[1::2])
33 md5string = ":".join(["".join(pair) for pair in pairs])
34
35 print(md5string)