def decrypt(self, from_path, to_path, encryption_data): if encryption_data["algorithm"] == SecurityAlgorithm.none: shutil.copyfile(from_path, to_path) return Result.success(True) cipher = self.get_cipher(encryption_data) if not cipher.is_ok(): return cipher cipher = cipher.result() decryptor = cipher.decryptor() block_size = cipher.algorithm.block_size padder = padding.PKCS7(block_size).unpadder() with open(from_path, "rb") as file: data = file.read() crypted = decryptor.update(data) padded_data = padder.update(crypted) padded_data += padder.finalize() with open(to_path, "wb") as file: file.write(padded_data) return Result.success(True)
def check_media(self, crypto_data): key_type = crypto_data["key_type"] media_path = crypto_data["media_path"] take_file_from_yadisk = crypto_data["take_file_from_yadisk"] if key_type in (KeyType.new_media, KeyType.existing_media): if take_file_from_yadisk: result = self.yadisk_model.download_media( media_path, self.temp_dir.name) #result = self.yadisk_model.download_media(media_path, "D:/yanileda/trash") return result else: return Result.success(media_path) else: return Result.success(media_path)
def check_url(self, url): logger.info("check url " + url) try: result = self.disk.public_exists(url) except NotFoundError: result = False return Result.success(result)
def set_verification_code(self, code): logger.info("Checking ver.code %s" % code) # time.sleep(5) try: response = self.disk.get_token(code) except yadisk.exceptions.BadRequestError: return Result.success(False) self.disk.token = response.access_token result = self.disk.check_token() if result: logger.info("Connected to Yadisk") else: logger.error("Cannot connect to Yadisk") return Result.success(result)
def set_media_folder(self, path): logger.info("set media folder %s" % path) if not self.disk.exists(path): return Result.failed("Media path '%s' doesn't exists" % path) if not self.disk.is_dir(path): return Result.failed("Media path '%s' isn't directory" % path) self.media_folder = path return Result.success()
def get_listdir(self, url): if self.check_url(url): if self.disk.is_public_dir(url): listdir = list(self.disk.public_listdir(url)) listdir.sort(key=lambda x: x.type == "dir", reverse=True) names = [self.header, ] for i in listdir: names.append(self.get_info(i)) return Result.success((names, url)) else: return Result.failed("Url isn't public") return Result.failed("Url doesnt't exist")
def get_listdir(self, path=None): logger.info("Open path %s" % path) if path is None: path = magic_const.YADISK_PREFIX if not self.disk.is_dir(path): return Result.failed("Path doesn't exist") names = [ self.header, ] listdir = list(self.disk.listdir(path)) listdir.sort(key=lambda x: x.type == "dir", reverse=True) for i in listdir: names.append(self.get_info(i)) return Result.success((names, path))
def download_media(self, media_name, dir_name): # check media media_path = Path(self.media_folder) / media_name logger.info("Check path %s" % media_path.as_posix()) if not self.disk.exists(media_path.as_posix()): return Result.failed("Media path isn't found") temp_path = Path(dir_name) / media_name download_result = self.download(media_path.as_posix(), temp_path.as_posix()) if download_result.is_ok(): if download_result.result(): return Result.success(temp_path.as_posix()) else: return Result.failed("Something has gone wrong") else: return download_result
def get_cipher(self, crypto_data): #assert len(encryption_data) == 4 algorithm_type = crypto_data["algorithm"] key_type = crypto_data["key_type"] media_path = crypto_data["media_path"] key = crypto_data["key"] media_type = crypto_data["media_type"] # get key from media if need it key = self.get_key(algorithm_type, key_type, key, media_path, media_type) if not key.is_ok(): return key else: key = key.result() algorithm = self.get_algorithm(algorithm_type, key) cipher = Cipher(algorithm, mode=modes.ECB(), backend=default_backend()) # print(cipher.algorithm.block_size) # print(cipher.block_size) return Result.success(cipher)
def download(self, from_path, to_path): if self.check_url(from_path): self.disk.download_public(from_path, to_path) return Result.success()
def get_meta(self, url): logger.info("get meta " + url) info = self.disk.get_public_meta(url) return Result.success(([self.header, (info.type, info.name, info.public_url, info.created.strftime(magic_const.DATETIME_FORMAT))], url))
def get_key(self, algorithm_type, key_type, key, media_path, media_type): logger.info("Key-file path %s" % media_path) if key_type == KeyType.new_symbols: if not key: return Result.failed("Why have you deleted the key?") return Result.success(key) elif key_type == KeyType.existing_symbols: if not key: return Result.failed("Write down password") return Result.success(key) elif key_type == KeyType.new_binary: if not media_path: return Result.failed("Select file for key") key = self.generate_key(algorithm_type) with open(media_path, "wb") as f: f.write(key) return Result.success(key) elif key_type == KeyType.existing_binary: if not media_path: return Result.failed("Select file with key") with open(media_path, "rb") as f: key = f.read() return Result.success(key) elif key_type == KeyType.new_media: if not key: return Result.failed("Write down password") if not media_path: return Result.failed("Select media file") if media_type == MediaType.f5steganography: key_key = self.generate_key(algorithm_type) embed_into_image(media_path, key_key.decode("utf-8"), key) elif media_type == MediaType.fractals: key_size = magic_const.KEY_LENGTH[algorithm_type] key_key = fractals.generate_key(media_path, key.decode("utf-8"), key_size) elif media_type == MediaType.dct: key_size = magic_const.KEY_LENGTH[algorithm_type] key_key = dct_algorithm.generate_key(media_path, key.decode("utf-8"), key_size) else: logger.error("Bad media type %s" % media_type) return Result.success(key_key) elif key_type == KeyType.existing_media: if not key: return Result.failed("Write down password") if not media_path: return Result.failed("Select media file") if media_type == MediaType.f5steganography: key_key = extract_from_image(media_path, key.decode("utf-8")) key_key = key_key.encode("utf-8") logger.info("Key len %d" % len(key_key)) elif media_type == MediaType.fractals: key_size = magic_const.KEY_LENGTH[algorithm_type] key_key = fractals.generate_key(media_path, key.decode("utf-8"), key_size) elif media_type == MediaType.dct: key_size = magic_const.KEY_LENGTH[algorithm_type] key_key = dct_algorithm.generate_key(media_path, key.decode("utf-8"), key_size) else: logger.error("Bad media type %s" % media_type) return Result.success(key_key)
def download(self, from_path, to_path): logger.info("download {} to {}".format(from_path, to_path)) self.disk.download(from_path, to_path) return Result.success()
def upload(self, from_path, to_path): logger.info("upload {} to {}".format(from_path, to_path)) self.disk.upload(from_path, to_path, overwrite=True) return Result.success()