def feed(self, data=None): '''Provide bytes to encrypt (or decrypt), returning any bytes possible from this or any previous calls to feed. Call with None or an empty string to flush the mode of operation and return any final bytes; no further calls to feed may be made.''' if self._buffer is None: raise ValueError('already finished feeder') # Finalize; process the spare bytes we were keeping if not data: result = self._final(self._buffer) self._buffer = None return result self._buffer += to_bufferable(data) # We keep 16 bytes around so we can determine padding result = to_bufferable('') while len(self._buffer) > 16: can_consume = self._mode._can_consume(len(self._buffer) - 16) if can_consume == 0: break result += self._feed(self._buffer[:can_consume]) self._buffer = self._buffer[can_consume:] return result
def _segment_final_decrypt(self, data): faux_padding = (chr(0) * (self.segment_bytes - (len(data) % self.segment_bytes))) padded = data + to_bufferable(faux_padding) return self.decrypt(padded)[:len(data)]
def __init__(self, mode, feed, final): self._mode = mode self._feed = feed self._final = final self._buffer = to_bufferable("")