smartinspectpython.sicryptostreamwriter

Module: sicryptostreamwriter.py

Revision History

Date Version Description
2023/05/30 3.0.0.0 Initial Version.

@export
class SICryptoStreamWriter(_io.BytesIO):

Cryptographic stream writer class used to encrypt data and write to a destination stream.

This class utilizes PyCryptodome cryptography support. More info can be found here: https://pycryptodome.readthedocs.io/en/latest/index.html

SICryptoStreamWriter(stream: _io.BufferedWriter, cipher, padMethod: str = 'pkcs7')

Initializes a new instance of the class.

Arguments:
  • stream (BufferedWriter): Destination stream (BufferedWriter object) that encrypted data will be written to.
  • cipher (object): AES Cipher object used to encrypt the data.
  • padMethod (str): Method used for padding bytes to be encoded; possible values are 'pkcs7' (default), 'iso7816' or 'x923'.
Raises:
  • SIArgumentNullException: Thrown if the cipher or stream argument is null.
def flush(self) -> None:

Overridden. Calls the same method on the destination stream specified at initialization.

def tell(self) -> int:

Overridden. Calls the same method on the destination stream specified at initialization.

def writable(self) -> bool:

Overridden. Calls the same method on the destination stream specified at initialization.

def write(self, data) -> int:

Overridden. Write encrypted data to the destination stream specified at initialization.

Arguments:
  • data (object): Data to write.

The data to write is encrypted using a block size of the Cipher specified at initialization, and written to the underlying stream. If there is not enough data to fill a block, then it is written to a temporary buffer to be processed on the next write or close.

def close(self) -> None:

Overridden. Calls the same method on the destination stream specified at initialization.

Prior to closing the destination stream, it will encrypt and write out any remaining bytes in the temporary buffer to the destination stream.