encrypt_str¶
-
EncryptHelper.
encrypt_str
(data: Union[str, bytes], key: Union[str, bytes] = None) → str[source]¶ Encrypts a piece of data
data
passed as a string or bytes using Fernet with the passed 32-bit symmetric encryption keykey
. Outputs the encrypted data as a Base64 string for easy storage.The
key
cannot just be a random “password”, it must be a 32-byte key encoded with URL Safe base64. Use the methodgenerate_key()
to create a Fernet compatible encryption key.Under the hood, Fernet uses AES-128 CBC to encrypt the data, with PKCS7 padding and HMAC_SHA256 authentication.
If the
key
parameter isn’t passed, or is empty (None / “”), then it will attempt to fall back toself.encrypt_key
- if that’s also empty, EncryptKeyMissing will be raised.- Parameters
data (str) – The data to be encrypted, in the form of either a str or bytes.
key (str) – A Fernet encryption key (base64) to be used, if left blank will fall back to
encrypt_key
- Raises
EncryptKeyMissing – Either no key was passed, or something is wrong with the key.
EncryptionError – Something went wrong while attempting to encrypt the data
- Return str encrypted_data
The encrypted version of the passed
data
as a base64 encoded string.