platypush/utils/__init__.py
def generate_rsa_key_pair(key_file: Optional[str] = None, size: int = 2048) -> Tuple[str, str]:
    """
    Generate an RSA key pair.

    :param key_file: Target file for the private key (the associated public key will be stored in ``<key_file>.pub``.
        If no key file is specified then the public and private keys will be returned in ASCII format in a dictionary
        with the following structure:

            .. code-block:: json

                {
                    "private": "private key here",
                    "public": "public key here"
                }

    :param size: Key size (default: 2048 bits).
    :return: A tuple with the generated ``(priv_key_str, pub_key_str)``.
    """
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography.hazmat.backends import default_backend

    public_exp = 65537
    private_key = rsa.generate_private_key(
        public_exponent=public_exp,
        key_size=size,
        backend=default_backend()
    )

    logger.info('Generating RSA {} key pair'.format(size))
    private_key_str = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption()
    ).decode()

    public_key_str = private_key.public_key().public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.PKCS1,
    ).decode()

    if key_file:
        logger.info('Saving private key to {}'.format(key_file))
        with open(os.path.expanduser(key_file), 'w') as f1, \
                open(os.path.expanduser(key_file) + '.pub', 'w') as f2:
            f1.write(private_key_str)
            f2.write(public_key_str)
            os.chmod(key_file, 0o600)

    return public_key_str, private_key_str

    Generate an RSA key pair.

    :param key_file: Target file for the private key (the associated public key will be stored in ``<key_file>.pub``.
        If no key file is specified then the public and private keys will be returned in ASCII format in a dictionary
        with the following structure:

            .. code-block:: json

                {
                    "private": "private key here",
                    "public": "public key here"
                }

    :param size: Key size (default: 2048 bits).
    :return: A tuple with the generated ``(priv_key_str, pub_key_str)``.
    
Generate an RSA key pair.
Generate an RSA key pair .
def generate_rsa_key_pair(key_file: Optional[str] = None, size: int = 2048) -> Tuple[str, str]:
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography.hazmat.backends import default_backend
    public_exp = 65537
    private_key = rsa.generate_private_key(
        public_exponent=public_exp,
        key_size=size,
        backend=default_backend()
    )
    logger.info('Generating RSA {} key pair'.format(size))
    private_key_str = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption()
    ).decode()
    public_key_str = private_key.public_key().public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.PKCS1,
    ).decode()
    if key_file:
        logger.info('Saving private key to {}'.format(key_file))
        with open(os.path.expanduser(key_file), 'w') as f1, \
                open(os.path.expanduser(key_file) + '.pub', 'w') as f2:
            f1.write(private_key_str)
            f2.write(public_key_str)
            os.chmod(key_file, 0o600)
    return public_key_str, private_key_str
def generate_rsa_key_pair ( key_file : Optional [ str ] = None , size : int = 2048 ) -> Tuple [ str , str ] : from cryptography . hazmat . primitives import serialization from cryptography . hazmat . primitives . asymmetric import rsa from cryptography . hazmat . backends import default_backend public_exp = 65537 private_key = rsa . generate_private_key ( public_exponent = public_exp , key_size = size , backend = default_backend ( ) ) logger . info ( 'Generating RSA {} key pair' . format ( size ) ) private_key_str = private_key . private_bytes ( encoding = serialization . Encoding . PEM , format = serialization . PrivateFormat . TraditionalOpenSSL , encryption_algorithm = serialization . NoEncryption ( ) ) . decode ( ) public_key_str = private_key . public_key ( ) . public_bytes ( encoding = serialization . Encoding . PEM , format = serialization . PublicFormat . PKCS1 , ) . decode ( ) if key_file : logger . info ( 'Saving private key to {}' . format ( key_file ) ) with open ( os . path . expanduser ( key_file ) , 'w' ) as f1 , open ( os . path . expanduser ( key_file ) + '.pub' , 'w' ) as f2 : f1 . write ( private_key_str ) f2 . write ( public_key_str ) os . chmod ( key_file , 0o600 ) return public_key_str , private_key_str
Generate an RSA key pair.
Generate an RSA key pair .
"""
    Generate an RSA key pair.

    :param key_file: Target file for the private key (the associated public key will be stored in ``<key_file>.pub``.
        If no key file is specified then the public and private keys will be returned in ASCII format in a dictionary
        with the following structure:

            .. code-block:: json

                {
                    "private": "private key here",
                    "public": "public key here"
                }

    :param size: Key size (default: 2048 bits).
    :return: A tuple with the generated ``(priv_key_str, pub_key_str)``.
    """
A tuple with the generated ``(priv_key_str, pub_key_str)``.
A tuple with the generated ` ` ( priv_key_str pub_key_str ) ` ` .
key_file
Optional[str]
Target file for the private key (the associated public key will be stored in ``.pub``.
If no key file is specified then the public and private keys will be returned in ASCII format in a dictionary
with the following structure.

code-block:: json

{
"private": "private key here",
"public": "public key here"
}
Target file for the private key ( the associated public key will be stored in ` ` . pub ` ` . If no key file is specified then the public and private keys will be returned in ASCII format in a dictionary with the following structure . code - block :: json { " private " : " private key here " " public " : " public key here " }
size
int
Key size (default: 2048 bits).
Key size ( default : 2048 bits ) .