Created
April 21, 2020 19:55
-
-
Save kgriffs/6c62b261a2b9b801d1713d482067f02f to your computer and use it in GitHub Desktop.
Python - Verify x509 cert matches private key using cryptography
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from cryptography import x509 | |
| from cryptography.hazmat.backends import default_backend | |
| from cryptography.hazmat.primitives import serialization | |
| def verify(path): | |
| with open(path, 'rb') as f: | |
| cert = x509.load_pem_x509_certificate(f.read(), default_backend()) | |
| with open(path, 'rb') as key_file: | |
| private_key = serialization.load_pem_private_key( | |
| key_file.read(), | |
| password=None, | |
| backend=default_backend() | |
| ) | |
| public_key = cert.public_key() | |
| return private_key.public_key().public_numbers() == public_key.public_numbers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment