xq.algorithms package

Submodules

xq.algorithms.aes_encryption module

class xq.algorithms.aes_encryption.AESEncryption(key: bytes, scheme: int = 1)

Bases: Encryption

AES implemented encryption algorithm

add_header_salt(header=None, salt_size=16, iv_size=12)

Generates a salt and IV, and adds them to the header

decrypt(data: bytes | object, password: str | None = None, salt_size=16, iv_size=12, chunk_size: int = 1048576, out_file=None)

Decrypts the provided data using AES-GCM (scheme 1) or AES-CTR (scheme 2).

derive_key(salt: bytes, password: bytes | None = None, iterations: int = 1024, key_length: int = 32)

Derives a key using PBKDF2 with HMAC-SHA256.

encrypt(data: str | bytes | object, password: str | None = None, header=None, chunk_size: int = 1048576, out_file=None)

Encrypts the provided data using AES-GCM (scheme 1) or AES-CTR (scheme 2).

xq.algorithms.encryption module

class xq.algorithms.encryption.Encryption(key: str)

Bases: object

parent class for all encryption algorithms

create_file_header(filename, token, scheme=1, version=1)
decryptFile(data, password: bytes | str | None = None, out_file=None, chunk_size: int = 1048576)
encryptFile(filename, data, token, password, scheme=1, *, out_file=None, chunk_size=1048576)

Encrypt a file payload with outer XQ header + inner body. - GCM (scheme==1): in-memory unless out_file handling upstream writes. - CTR (scheme==2): streams when out_file is provided. - OTP (scheme==’B’): now supported; can stream to out_file too. :param data: file-like or bytes/str

property key

method property that returns the correct key value used for encryption

Returns:

key used for encryption

Return type:

bytes

shuffle(string: str | None = None)

psudo-randomize a provided string * replicated from jssdk-core

Parameters:

string (str) – provided string to randomize

Returns:

randomized string

Return type:

str

xq.algorithms.otp_encryption module

class xq.algorithms.otp_encryption.OTPEncryption(key: bytes, max_encryption_chunk_size=2048)

Bases: Encryption

OTP implimented encryption algorithm

Parameters:

Encryption (Encryption class) – Inherited Parent class

decrypt(text: bytes, password: bytes | None = None) bytes

decryption method for decrypting a string or file

Parameters:

text (bytes) – text to decrypt

Returns:

decrypted text

Return type:

bytes

decrypt_chunk(chunk: bytes, password: bytes, key_offset: int)

Decrypt a single chunk using XOR with password at given offset. Since XOR is symmetric, this is identical to encrypt_chunk.

Parameters:
  • chunk – Data chunk to decrypt

  • password – Decryption password

  • key_offset – Current offset in the password key

Returns:

Decrypted chunk as bytes

decrypt_file_streaming(file: BinaryIO | BufferedReader | BytesIO, password: str | bytes)

Stream decrypt a file using OTP (XOR) encryption. Reads file in chunks to avoid loading entire file into memory.

Parameters:
  • file – File object to decrypt (must have header)

  • password – Decryption password

Returns:

Decrypted file as bytes

encrypt(msg: bytes, password: bytes | None = None)

encryption method for encrypting a bytes-string or bytes-file

Parameters:

msg (bytes OR FileLike) – message to encrypt

Raises:

SDKEncryptionException – unsupported message type

Returns:

encrypted message

Return type:

bytes

encrypt_chunk(chunk: bytes, password: bytes, key_offset: int) bytes

Encrypt a single chunk using XOR with password at given offset.

Parameters:
  • chunk – Data chunk to encrypt

  • password – Encryption password

  • key_offset – Current offset in the password key

Returns:

Encrypted chunk as bytes

encrypt_file_streaming(file: BinaryIO | BufferedReader | BytesIO | bytes | bytearray | str | PathLike, password: str | bytes, header: bytes, out_file: BinaryIO | None = None, chunk_size: int = 1048576)

Stream encrypt a file using OTP (XOR) encryption. Reads file in chunks to avoid loading entire file into memory.

Parameters:
  • file – File object to encrypt

  • password – Encryption password

  • header – File header to prepend

Returns:

Encrypted file as bytes

Module contents