Debian Codegraph @

ansible_collections/amazon/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 import hashlib
12 import sys
13
14 from cryptography.hazmat.primitives import serialization
15
16 if len(sys.argv) == 0:
17 ssh_public_key = "id_rsa.pub"
18 else:
19 ssh_public_key = sys.argv[1]
20
21 with open(ssh_public_key, "rb") as key_file:
22 public_key = serialization.load_ssh_public_key(
23 key_file.read(),
24 )
25 pub_der = public_key.public_bytes(
26 encoding=serialization.Encoding.DER,
27 format=serialization.PublicFormat.SubjectPublicKeyInfo,
28 )
29 md5digest = hashlib.md5(pub_der).hexdigest()
30 # Format the md5sum into the normal format
31 pairs = zip(md5digest[::2], md5digest[1::2])
32 md5string = ":".join(["".join(pair) for pair in pairs])
33
34 print(md5string)