vantage6.common.encryption.CryptorBase

class CryptorBase(*args, **kwargs)

Bases: object

Base class/interface for encryption implementations.

__init__()

Create a new CryptorBase instance.

Methods

__init__()

Create a new CryptorBase instance.

bytes_to_str(data)

Encode bytes as base64 encoded string.

decrypt(data)

Decrypt base64 encoded string data.

decrypt_stream(stream[, chunk_size])

Decode a base64-encoded stream to bytes, yielding decoded chunks.

encrypt_bytes_to_str(data, pubkey_base64[, ...])

Encrypt bytes in data using a (base64 encoded) public key.

encrypt_stream(stream[, pubkey_base64s, ...])

Base64-encode a stream, yielding encoded chunks.

str_to_bytes(data)

Decode base64 encoded string to bytes.

static bytes_to_str(data)

Encode bytes as base64 encoded string.

Parameters:

data (bytes) – The data to encode.

Returns:

The base64 encoded string.

Return type:

str

decrypt(data)

Decrypt base64 encoded string data.

Parameters:

data (str | bytes) – The data to decrypt. Can be either a string or bytes, depending on whether the data comes from blob storage or not.

Returns:

The decrypted data.

Return type:

bytes

decrypt_stream(stream, chunk_size=1048576)

Decode a base64-encoded stream to bytes, yielding decoded chunks. Naming here is confusing (this function does not decrypt), but it is kept for compatibility with the RSACryptor class, as well as staying consistent with existing cryptorbase method names like decrypt_str_to_bytes.

Parameters:
  • stream (file-like) – The input stream to decode (must support .read()).

  • chunk_size (int) – The size of chunks to read and decode.

Yields:

bytes – Decoded data chunks.

encrypt_bytes_to_str(data, pubkey_base64, skip_base64_encoding_of_msg=False)

Encrypt bytes in data using a (base64 encoded) public key.

Note that the public key is ignored in this base class. If you want to encode your data with a public key, use the RSACryptor class.

Parameters:
  • data (bytes) – The data to encrypt.

  • pubkey_base64 (str) – The public key to use for encryption. This is ignored in this base class.

  • skip_base64_encoding_of_msg (bool) – If True, the encrypted message will not be base64 encoded. This is useful when the data is already in bytes format and does not need further encoding (e.g., when uploading to blob storage).

Returns:

The encrypted data encoded as base64 string.

Return type:

str

encrypt_stream(stream, pubkey_base64s=None, chunk_size=1048576)

Base64-encode a stream, yielding encoded chunks. Naming here is confusing (this function does not encrypt), but it is kept for compatibility with the RSACryptor class, as well as staying consistent with existing cryptorbase method names like encrypt_bytes_to_str.

Parameters:
  • stream (file-like) – The input stream to encode (must support .read()).

  • pubkey_base64s (str) – Ignored. Only used in RSACryptor for encryption.

  • chunk_size (int) – The size of chunks to read and encode.

Yields:

bytes – Base64-encoded data chunks.

static str_to_bytes(data)

Decode base64 encoded string to bytes.

Parameters:

data (str) – The base64 encoded string.

Returns:

The encoded string converted to bytes.

Return type:

bytes