def _load_pkcs1_der(cls, keyfile): """Loads a key in PKCS#1 DER format. :param keyfile: contents of a DER-encoded file that contains the public key. :return: a PublicKey object First let's construct a DER encoded key: >>> import base64 >>> b64der = 'MAwCBQCNGmYtAgMBAAE=' >>> der = base64.standard_b64decode(b64der) This loads the file: >>> PublicKey._load_pkcs1_der(der) PublicKey(2367317549, 65537) """ # pylint: disable=import-outside-toplevel from adafruit_rsa.tools.pyasn1.codec.der import decoder from adafruit_rsa.asn1 import AsnPubKey (priv, _) = decoder.decode(keyfile, asn1Spec=AsnPubKey()) return cls(n=int(priv["modulus"]), e=int(priv["publicExponent"]))
def _save_pkcs1_der(self): """Saves the public key in PKCS#1 DER format. :returns: the DER-encoded public key. :rtype: bytes """ # pylint: disable=import-outside-toplevel from pyasn1.codec.der import encoder from rsa.asn1 import AsnPubKey # Create the ASN object asn_key = AsnPubKey() asn_key.setComponentByName("modulus", self.n) asn_key.setComponentByName("publicExponent", self.e) return encoder.encode(asn_key)
def _save_pkcs1_der(self): """Saves the public key in PKCS#1 DER format. :returns: the DER-encoded public key. :rtype: bytes """ from pyasn1.codec.der import encoder from rsa.asn1 import AsnPubKey # Create the ASN object asn_key = AsnPubKey() asn_key.setComponentByName('modulus', self.n) asn_key.setComponentByName('publicExponent', self.e) return encoder.encode(asn_key)