# Path: studio/ed25519_key_util.py
# class Ed25519KeyUtil:
# 
#     @classmethod
#     def parse_private_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 private key in OPEN SSH PRIVATE key format.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: (public key part as bytes, private key part as bytes)
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = pkl.PrivateKeyList.from_string(contents)
#             data_public = key_data[0].private.params['public']
#             data_private = key_data[0].private.params['private_public']
#             return data_public, data_private[:32]
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None, None
# 
#     @classmethod
#     def parse_public_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 public.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: public key part as bytes
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = PublicKey.from_string(contents)
#             data_public = key_data.params['public']
#             return data_public
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None
# 
# Path: studio/payload_builders/payload_builder.py
# class PayloadBuilder:
#     """
#     Abstract class representing
#     payload object construction from experiment components.
#     Result is payload ready to be submitted for execution.
#     """
#     def __init__(self, name: str):
#         self.name = name if name else 'NO NAME'
# 
#     def construct(self, experiment, config, packages):
#         raise NotImplementedError(
#             'Not implemented for payload builder {0}'.format(self.name))
# 
# Path: studio/util/logs.py
# DEBUG = logging.DEBUG
# INFO = logging.INFO
# ERROR = logging.ERROR
# def get_logger(name):
# def debug(line):
# def error(line):
# def info(line):
# 
# Path: studio/payload_builders/unencrypted_payload_builder.py
# class UnencryptedPayloadBuilder(PayloadBuilder):
#     """
#     Simple payload builder constructing
#     unencrypted experiment payloads.
#     """
#     def construct(self, experiment: Experiment, config, packages):
#         return { 'experiment': experiment.to_dict(),
#                  'config': config}
# 
# Path: studio/util/util.py
# def check_for_kb_interrupt():
#     current_exc = sys.exc_info()[1]
#     if current_exc is None:
#         return
#     if isinstance(current_exc, KeyboardInterrupt):
#         raise current_exc
#     return


from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from nacl.signing import SigningKey, VerifyKey
from sshpubkeys import SSHKey
from studio.ed25519_key_util import Ed25519KeyUtil
from studio.payload_builders.payload_builder import PayloadBuilder
from studio.util import logs
from studio.payload_builders.unencrypted_payload_builder import UnencryptedPayloadBuilder
from studio.util.util import check_for_kb_interrupt
import nacl.secret
import nacl.utils
import base64
import json
            self._rsa_encrypt_data_to_base64(self.recipient_key, session_key)

        return encrypted_session_key_text, encrypted_data_text

    def _sign_payload(self, encrypted_payload):
        """
        encrypted_payload - base64 representation of the encrypted payload.
        returns: base64-encoded signature
        """
        sign_message = self.sender_key.sign(encrypted_payload)

        # Verify what we generated just in case:
        try:
            self.verify_key.verify(sign_message)
        except Exception as exc:
            msg: str = "FAILED to verify signed data - {0}. ABORTING."\
                .format(exc)
            self._raise_error(msg)

        result = base64.b64encode(sign_message.signature)
        return result

    def _decrypt_data(self, private_key_path, encrypted_key_text, encrypted_data_text):
        private_key = self._import_rsa_key(private_key_path)
        if private_key is None:
            return None

        try:
            private_key = RSA.import_key(open(private_key_path).read())
        except:

# Path: studio/ed25519_key_util.py
# class Ed25519KeyUtil:
# 
#     @classmethod
#     def parse_private_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 private key in OPEN SSH PRIVATE key format.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: (public key part as bytes, private key part as bytes)
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = pkl.PrivateKeyList.from_string(contents)
#             data_public = key_data[0].private.params['public']
#             data_private = key_data[0].private.params['private_public']
#             return data_public, data_private[:32]
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None, None
# 
#     @classmethod
#     def parse_public_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 public.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: public key part as bytes
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = PublicKey.from_string(contents)
#             data_public = key_data.params['public']
#             return data_public
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None
# 
# Path: studio/payload_builders/payload_builder.py
# class PayloadBuilder:
#     """
#     Abstract class representing
#     payload object construction from experiment components.
#     Result is payload ready to be submitted for execution.
#     """
#     def __init__(self, name: str):
#         self.name = name if name else 'NO NAME'
# 
#     def construct(self, experiment, config, packages):
#         raise NotImplementedError(
#             'Not implemented for payload builder {0}'.format(self.name))
# 
# Path: studio/util/logs.py
# DEBUG = logging.DEBUG
# INFO = logging.INFO
# ERROR = logging.ERROR
# def get_logger(name):
# def debug(line):
# def error(line):
# def info(line):
# 
# Path: studio/payload_builders/unencrypted_payload_builder.py
# class UnencryptedPayloadBuilder(PayloadBuilder):
#     """
#     Simple payload builder constructing
#     unencrypted experiment payloads.
#     """
#     def construct(self, experiment: Experiment, config, packages):
#         return { 'experiment': experiment.to_dict(),
#                  'config': config}
# 
# Path: studio/util/util.py
# def check_for_kb_interrupt():
#     current_exc = sys.exc_info()[1]
#     if current_exc is None:
#         return
#     if isinstance(current_exc, KeyboardInterrupt):
#         raise current_exc
#     return


# Path: studio/encrypted_payload_builder.py
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from nacl.signing import SigningKey, VerifyKey
from sshpubkeys import SSHKey
from studio.ed25519_key_util import Ed25519KeyUtil
from studio.payload_builders.payload_builder import PayloadBuilder
from studio.util import logs
from studio.payload_builders.unencrypted_payload_builder import UnencryptedPayloadBuilder
from studio.util.util import check_for_kb_interrupt
import nacl.secret
import nacl.utils
import base64
import json

            self._rsa_encrypt_data_to_base64(self.recipient_key, session_key)

        return encrypted_session_key_text, encrypted_data_text

    def _sign_payload(self, encrypted_payload):
        """
        encrypted_payload - base64 representation of the encrypted payload.
        returns: base64-encoded signature
        """
        sign_message = self.sender_key.sign(encrypted_payload)

        # Verify what we generated just in case:
        try:
            self.verify_key.verify(sign_message)
        except Exception as exc:
            msg: str = "FAILED to verify signed data - {0}. ABORTING."\
                .format(exc)
            self._raise_error(msg)

        result = base64.b64encode(sign_message.signature)
        return result

    def _decrypt_data(self, private_key_path, encrypted_key_text, encrypted_data_text):
        private_key = self._import_rsa_key(private_key_path)
        if private_key is None:
            return None

        try:
            private_key = RSA.import_key(open(private_key_path).read())
        except:

            check_for_kb_interrupt()