def _generate_keypair() -> _Keypair:
    """Generate RSA keypair as bytes in OpenSSH format."""
    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=4096,
    )
    private_key_bytes = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    )
    public_key_bytes = private_key.public_key().public_bytes(
        encoding=serialization.Encoding.OpenSSH,  # paramiko flakiness fix
        format=serialization.PublicFormat.OpenSSH,
    )
    return _Keypair(
        private_key=private_key_bytes,
        public_key=public_key_bytes,
    )
Generate RSA keypair as bytes in OpenSSH format.
modules/sftp/testcontainers/sftp/__init__.py