:py:mod:`pylibressl.rsa` ======================== .. py:module:: pylibressl.rsa .. autoapi-nested-parse:: RSA signing and encryption. Contains routines to sign/verify, encrypt/decrypt messages using RSA and a wrapper class to store RSA keypair. Also it supports generation of RSA keys with custom length and exponent. Note that only keys in PEM format are supported. Signing example: >>> from pylibressl.rsa import RSAKeypair, RSASign_SHA512 >>> >>> privkey = open('private_key.pem', 'rb').read() >>> keypair = RSAKeypair(private_key=privkey) >>> signer = RSASign_SHA512(keypair) >>> >>> message = b'Example message. 1234567890' >>> signature = signer.sign(message) >>> >>> if signer.verify(message, signature): ... print('Signature is ok') >>> else: ... print('Signature is NOT ok!!!') Cipher example: >>> from pylibressl.rsa import RSAKeypair, RSACrypt_AES256 >>> >>> privkey = open('private_key.pem', 'rb').read() >>> keypair = RSAKeypair(private_key=privkey) >>> rsacrypt = RSACrypt_AES256(keypair) >>> >>> message = b'Example message. 1234567890' >>> enc_message, session_key, iv = rsacrypt.encrypt(message) >>> decoded_message = rsacrypt.decrypt(enc_message, session_key, iv) >>> assert decoded_message == message Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 cipher/index.rst keygen/index.rst keypair/index.rst sign/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: pylibressl.rsa.RSAKeypair pylibressl.rsa.RSASign pylibressl.rsa.RSACrypt Functions ~~~~~~~~~ .. autoapisummary:: pylibressl.rsa.public_from_private pylibressl.rsa.generate_rsa_key Attributes ~~~~~~~~~~ .. autoapisummary:: pylibressl.rsa.RSASign_SHA512 pylibressl.rsa.RSACrypt_AES256 .. py:class:: RSAKeypair(public_key=None, private_key=None) Bases: :py:obj:`object` RSA keypair container. .. py:method:: _set_one_key(self, key, is_public) .. py:method:: _set_pkey(self, public_key, private_key) .. py:method:: has_private_key(self) Returns True if private key is present in keypair. .. py:method:: key_size(self) Get key size (actually, modulus length) in bytes. .. py:function:: public_from_private(private_key) Derive public key from private one. .. py:class:: RSASign(rsa_keypair) Bases: :py:obj:`object` RSA signing class. .. py:method:: new(cls, digest_type, name='NewRSASign') :classmethod: Create new RSA signing class. .. py:method:: sign(self, message) Sign a message with RSA. .. py:method:: verify(self, message, signature) Verify signed message with RSA. .. py:data:: RSASign_SHA512 .. py:class:: RSACrypt(keypair) Bases: :py:obj:`object` RSA en/decryption class. .. py:method:: new(cls, symmetric_cipher, name='NewRSACrypt') :classmethod: Create new RSA cipher class. .. py:method:: encrypt(self, data) .. py:method:: decrypt(self, data, session_key, iv) .. py:data:: RSACrypt_AES256 .. py:function:: generate_rsa_key(bits=2048, exponent=65537) Generate RSA key. :param bits: key length in bits :param exponent: exponent value, should be odd :returns: private key bytestring in PEM format