Exemple #1
0
class Encoder:

    MODES = {'GCM': 8}

    def __init__(self,
                 password,
                 source_image_folder_path,
                 destination_image_folder_path,
                 mode='GCM'):
        self.mode = Encoder.MODES[mode]
        self.source_image_folder_path = source_image_folder_path
        self.destination_image_folder_path = destination_image_folder_path
        if os.path.exists(self.destination_image_folder_path):
            shutil.rmtree(self.destination_image_folder_path)
        os.makedirs(self.destination_image_folder_path)

        self.gcm = GCM()
        self.gcm.make_key(password)
        self.transcriber = Transcriber(self.mode, source_image_folder_path,
                                       destination_image_folder_path)

    def encode_file(self, file_path):
        with open(file_path, 'rb') as source:
            file_bytes = source.read()
            encrypted = self.gcm.encrypt(file_bytes)
            tag = self.gcm.encrypt_finalize()

        header = self.gcm.password_salt + self.gcm.iv

        full_length = len(header) + len(encrypted) + len(tag)
        # if not self.transcriber.can_fit_bytes(full_length):
        #     print("Not enough images to store the necessary data")
        #     exit()
        self.transcriber.choose_needed_images(full_length)

        print("full length", full_length, full_length * 8)

        self.transcriber.set_encoding(header + encrypted + tag)
        # with open("encode-test", "wb") as f:
        #     f.write(header + encrypted + tag)
        self.transcriber.finish_encoding()