Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Created April 21, 2020 19:55
Show Gist options
  • Select an option

  • Save kgriffs/6c62b261a2b9b801d1713d482067f02f to your computer and use it in GitHub Desktop.

Select an option

Save kgriffs/6c62b261a2b9b801d1713d482067f02f to your computer and use it in GitHub Desktop.
Python - Verify x509 cert matches private key using cryptography
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