# -*- coding: utf-8 -*- import hashlib import io import struct # default from KeePass2 source BLOCK_LENGTH = 1024 * 1024 try: file_types = (file, io.IOBase) except NameError: file_types = (io.IOBase,) # HEADER_LENGTH = 4+32+4 def read_int(stream, length): try: return struct.unpack(' 0: data = block_stream.read(length) if hashlib.sha256(data).digest() == bhash: return data else: raise IOError('Block hash mismatch error.') return bytes() def write_block_stream(self, stream, block_length=BLOCK_LENGTH): """ Write all data in this buffer, starting at stream position 0, formatted in hashed blocks to the given `stream`. For example, writing data from one file into another as hashed blocks:: # create new hashed block io without input stream or data hb = HashedBlockIO() # read from a file, write into the empty hb with open('sample.dat', 'rb') as infile: hb.write(infile.read()) # write from the hb into a new file with open('hb_sample.dat', 'w') as outfile: hb.write_block_stream(outfile) """ if not (isinstance(stream, io.IOBase) or isinstance(stream, file_types)): raise TypeError('Stream does not have the buffer interface.') index = 0 self.seek(0) while True: data = self.read(block_length) if data: stream.write(struct.pack('