#!/usr/bin/env python import os import sys from base64 import b64encode from uuid import uuid4 try: from cryptography import x509 from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes except ImportError: print("Please install cryptography: `pip install cryptography`") sys.exit(1) if len(sys.argv) < 2 or not os.path.exists(sys.argv[1]): print("Specify the path to the certificate as the first argument:\n" "\tpython %s /path/to/cert" % sys.argv[0].rsplit('/')[-1]) sys.exit(1) with open(sys.argv[1], 'rb') as fp: _cert_string = fp.read() _cert_x509 = x509.load_pem_x509_certificate(_cert_string, default_backend()) cert_fp_hash = b64encode(_cert_x509.fingerprint(hashes.SHA1())) cert_base64 = _cert_string.replace(b'\n', b'') cert_base64 = cert_base64.replace(b'-----BEGIN CERTIFICATE-----', b'') cert_base64 = cert_base64.replace(b'-----END CERTIFICATE-----', b'') print('SHA1 hash of certificate fingerprint: %s' % cert_fp_hash.decode()) print('Key ID: %s' % uuid4()) print('Base64 encoded certificate:\n%s\n' % cert_base64.decode())