Skip to content

Instantly share code, notes, and snippets.

@jcfrank
Forked from vinodc/azure-key-credentials.py
Last active November 3, 2022 07:16
Show Gist options
  • Save jcfrank/003cc2d75d0ffb28ae739ae2bf3283e3 to your computer and use it in GitHub Desktop.
Save jcfrank/003cc2d75d0ffb28ae739ae2bf3283e3 to your computer and use it in GitHub Desktop.

Revisions

  1. jcfrank revised this gist Aug 26, 2020. 1 changed file with 19 additions and 8 deletions.
    27 changes: 19 additions & 8 deletions azure-key-credentials.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/bin/env python
    #!/usr/bin/env python3

    import os
    import sys
    @@ -22,11 +22,22 @@
    _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'')
    cert_fp_hash = b64encode(_cert_x509.fingerprint(hashes.SHA1())).decode()
    cert_base64 = _cert_string.decode().replace('\n', '')
    cert_base64 = cert_base64.replace('-----BEGIN CERTIFICATE-----', '')
    cert_base64 = cert_base64.replace('-----END CERTIFICATE-----', '')
    key_id = uuid4()

    print('Custom Key Identifier (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())
    print(f'Custom Key Identifier (SHA1 hash of certificate fingerprint): {cert_fp_hash}')
    print(f'Key ID: {key_id}')
    print(f'Base64 encoded certificate:\n{cert_base64}\n')

    print(f'''Azure App credentials format:
    {{
    "customKeyIdentifier": "{cert_fp_hash}",
    "keyId": "{key_id}",
    "type": "AsymmetricX509Cert",
    "usage": "Verify",
    "value": "{cert_base64}"
    }}
    ''')
  2. @vinodc vinodc revised this gist Aug 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion azure-key-credentials.py
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,6 @@
    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('Custom Key Identifier (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())
  3. @vinodc vinodc revised this gist Jul 7, 2020. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions azure-key-credentials.py
    Original file line number Diff line number Diff line change
    @@ -20,13 +20,13 @@

    with open(sys.argv[1], 'rb') as fp:
    _cert_string = fp.read()
    _cert_x509 = x509.load_pem_x509_certificate(
    str.encode(_cert_string), default_backend())
    _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('\n', '')
    cert_base64 = cert_base64.replace('-----BEGIN CERTIFICATE-----', '')
    cert_base64 = cert_base64.replace('-----END CERTIFICATE-----', '')
    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)
    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)
    print('Base64 encoded certificate:\n%s\n' % cert_base64.decode())
  4. @vinodc vinodc created this gist Aug 23, 2017.
    32 changes: 32 additions & 0 deletions azure-key-credentials.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/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(
    str.encode(_cert_string), default_backend())
    cert_fp_hash = b64encode(_cert_x509.fingerprint(hashes.SHA1()))
    cert_base64 = _cert_string.replace('\n', '')
    cert_base64 = cert_base64.replace('-----BEGIN CERTIFICATE-----', '')
    cert_base64 = cert_base64.replace('-----END CERTIFICATE-----', '')

    print('SHA1 hash of certificate fingerprint: %s' % cert_fp_hash)
    print('Key ID: %s' % uuid4())
    print('Base64 encoded certificate:\n%s\n' % cert_base64)