EncryptHelper¶
-
class
privex.helpers.crypto.EncryptHelper.
EncryptHelper
(encrypt_key: str, **kwargs)[source]¶ Symmetric AES-128 encryption/decryption made painless - wrapper class for
cryptography.fernet.Fernet
A wrapper class for the
cryptography.fernet.Fernet
encryption system, designed to make usage of Fernet as painless as possible.The class
EncryptHelper
contains various methods for simplifying the use of the Python library Cryptography ‘scryptography.fernet.Fernet
system.encrypt_str()
/decrypt_str()
facilitate painless encryption and decryption of data using AES-128 CBC. They can either be passed a 32-byte Fernet key (base64 encoded) as an argument, or leave the key as None and they’ll try to use the key defined on theEncryptHelper
instance atEncryptHelper.encrypt_key
Basic usage:
>>> from privex.helpers import EncryptHelper >>> key = EncryptHelper.generate_key() # Generates a 32-byte symmetric key, returned as a base64 encoded string >>> crypt = EncryptHelper(key) # Create an instance of EncryptHelper, en/decrypting using ``key`` by default # Encrypts the string 'hello world' with AES-128 CBC using the instance's key, returned as a base64 string >>> enc = crypt.encrypt_str('hello world') >>> print(enc) gAAAAABc7ERTpu2D_uven3l-KtU_ewUC8YWKqXEbLEKrPKrKWT138MNq-I9RRtCD8UZLdQrcdM_IhUU6r8T16lQkoJZ-I7N39g==
>>> crypt.is_encrypted(enc) # Check if a string/bytes is encrypted (only works with data matching the key) True >>> data = crypt.decrypt_str(enc) # Decrypt the encrypted data using the same key, outputs as a string >>> print(data) hello world
-
__init__
(encrypt_key: str, **kwargs)[source] Create an instance of
EncryptHelper
using thecryptography.fernet.Fernet
keyencrypt_key
as the default key for encrypting/decrypting data.- Parameters
encrypt_key (str) – Base64 encoded Fernet key, used by default for encrypting/decrypting data
-
Methods¶
Methods
|
Create an instance of |
|
Decrypts |
|
Encrypts a piece of data |
|
Create an instance of |
|
Create an instance of |
|
Generate a compatible encryption key for use with |
|
Used internally for getting Fernet instance with auto-fallback to |
|
Returns True if the passed |
|
Generate a |