crypto: add property for private_key
This commit is contained in:
		@ -4,6 +4,8 @@ from typing import Optional
 | 
			
		||||
 | 
			
		||||
from cryptography.hazmat.backends import default_backend
 | 
			
		||||
from cryptography.hazmat.primitives import hashes
 | 
			
		||||
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
 | 
			
		||||
from cryptography.hazmat.primitives.serialization import load_pem_private_key
 | 
			
		||||
from cryptography.x509 import Certificate, load_pem_x509_certificate
 | 
			
		||||
from django.db import models
 | 
			
		||||
from django.utils.translation import gettext_lazy as _
 | 
			
		||||
@ -26,6 +28,7 @@ class CertificateKeyPair(UUIDModel, CreatedUpdatedModel):
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    _cert: Optional[Certificate] = None
 | 
			
		||||
    _key: Optional[RSAPrivateKey] = None
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def certificate(self) -> Certificate:
 | 
			
		||||
@ -36,6 +39,17 @@ class CertificateKeyPair(UUIDModel, CreatedUpdatedModel):
 | 
			
		||||
            )
 | 
			
		||||
        return self._cert
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def private_key(self) -> Optional[RSAPrivateKey]:
 | 
			
		||||
        """Get python cryptography PrivateKey instance"""
 | 
			
		||||
        if not self._key:
 | 
			
		||||
            self._key = load_pem_private_key(
 | 
			
		||||
                str.encode("\n".join([x.strip() for x in self.key_data.split("\n")])),
 | 
			
		||||
                password=None,
 | 
			
		||||
                backend=default_backend(),
 | 
			
		||||
            )
 | 
			
		||||
        return self._key
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def fingerprint(self) -> str:
 | 
			
		||||
        """Get SHA256 Fingerprint of certificate_data"""
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user