def compress_and_encrypt_db(self, password: str) -> Tuple[B64EncodedBytes, str]: """Decrypt the DB, dump in temporary plaintextdb, compress it, and then re-encrypt it Returns a b64 encoded binary blob""" log.info('Compress and encrypt DB') compressor = zlib.compressobj(level=9) with tempfile.TemporaryDirectory() as tmpdirname: tempdb = Path(tmpdirname) / 'temp.db' self.db.export_unencrypted(tempdb) source_data = bytearray() compressed_data = bytearray() with open(tempdb, 'rb') as src_f: block = src_f.read(BUFFERSIZE) while block: source_data += block compressed_data += compressor.compress(block) block = src_f.read(BUFFERSIZE) compressed_data += compressor.flush() original_data_hash = base64.b64encode( hashlib.sha256(source_data).digest(), ).decode() encrypted_data = encrypt(password.encode(), bytes(compressed_data)) return B64EncodedBytes(encrypted_data.encode()), original_data_hash
def compress_and_encrypt_db(self, password: str) -> Tuple[B64EncodedBytes, str]: """Decrypt the DB, dump in temporary plaintextdb, compress it, and then re-encrypt it Returns a b64 encoded binary blob""" log.info('Compress and encrypt DB') with tempfile.TemporaryDirectory() as tmpdirname: tempdb = FilePath(os.path.join(tmpdirname, 'temp.db')) self.db.export_unencrypted(tempdb) with open(tempdb, 'rb') as f: data_blob = f.read() original_data_hash = base64.b64encode( hashlib.sha256(data_blob).digest(), ).decode() compressed_data = zlib.compress(data_blob, level=9) encrypted_data = encrypt(password.encode(), compressed_data) return B64EncodedBytes(encrypted_data.encode()), original_data_hash