encrypt_str¶
-
EncryptHelper.encrypt_str(data: Union[str, bytes], key: Union[str, bytes] = None) → str[source]¶ Encrypts a piece of data
datapassed 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
keycannot 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
keyparameter 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
- 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
dataas a base64 encoded string.