privex.helpers.crypto.base¶
Various small helper functions/classes often used by classes such as EncryptHelper
and KeyManager
Functions
|
Determines if |
|
Returns |
-
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
data (str|bytes) – Data to check for base64, and decode if required
urlsafe (bool) – (Default:
True
) Usebase64.urlsafe_b64decode()
, instead of plainbase64.b64decode()
- 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
ifsb
appears to be a Base64 encoded string/bytes, otherwiseFalse
- Parameters
sb (str|bytes) – Data to check for base64 encoding
urlsafe (bool) – (Default:
True
) Usebase64.urlsafe_b64encode()
and decode, instead of plainbase64.b64encode()
and decode.
- Return bool is_base64
True
if data appears to be Base64, otherwiseFalse