示例#1
0
    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
示例#2
0
    def compress_and_encrypt_db(self, password: str) -> Tuple[bytes, str]:
        """Decrypt the DB, dump in temporary plaintextdb, compress it,
        and then re-encrypt it

        Returns a b64 encoded binary blob"""
        with tempfile.TemporaryDirectory() as tmpdirname:
            tempdb = cast(typing.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 encrypted_data.encode(), original_data_hash
示例#3
0
    def compress_and_encrypt_db(self, password):
        """ Decrypt the DB, dump in temporary plaintextdb, compress it,
        and then re-encrypt it

        Returns a b64 encoded binary blob"""
        with tempfile.TemporaryDirectory() as tmpdirname:
            tempdb = 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)
        print('COMPRESSED-ENCRYPTED LENGTH: {}'.format(len(encrypted_data)))

        return encrypted_data.encode(), original_data_hash
示例#4
0
    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 = Path(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