privex.helpers.crypto.base

Various small helper functions/classes often used by classes such as EncryptHelper and KeyManager

Functions

auto_b64decode(data[, urlsafe])

Determines if data is base64 encoded.

is_base64(sb[, urlsafe])

Returns True if sb appears to be a Base64 encoded string/bytes, otherwise False

privex.helpers.crypto.base.auto_b64decode(data: Union[str, bytes], urlsafe: bool = True)bytes[source]

Determines if data is base64 encoded. If it is, then it will decode it and return the decoded bytes. Otherwise, it will return the original data as bytes.

Example usage

>>> a = b'dGVzdGluZw=='
>>> b = 'hello world'
>>> auto_b64decode(a)  # This is detected to be Base64 encoded, and so the decoded data is returned
b'testing'
>>> auto_b64decode(b)  # This does not appear to be Base64, and so is returned as normal (but as bytes)
b'hello world'
Parameters
Return bytes data

If data was base64, this will be the decoded bytes. Otherwise, the original data as bytes.

privex.helpers.crypto.base.is_base64(sb: Union[str, bytes], urlsafe: bool = True)bool[source]

Returns True if sb appears to be a Base64 encoded string/bytes, otherwise False

Parameters
Return bool is_base64

True if data appears to be Base64, otherwise False