symmetric.py

Contains functions for symmetric encryption/decryption, by default with AES_256_CBC although this can be set to an other encryption algorithm or key length in a later stage.

Note

The encrypted values are returned with the cipher prepended, as following: AES256CBC$encrypted_value

Class and function definitions:

exception core.encryption.symmetric.EncryptionException[source]

Bases: exceptions.Exception

This exception is thrown when the cipher is unknown.

core.encryption.symmetric.get_max_length(max_length, cipher=<class core.encryption.AES.AES_256_CBC>)[source]

Get the max_length to set on a field based on the max_length you want to store on the field..

Args:
  • max_length: the max_length of the text to store
  • cipher: override the default cipher
Returns:
The max length of the field to hold the AES encryption. based on the first: 2 ^ x which is high enough.
Raises:
EncryptionException: Cipher is unknown
core.encryption.symmetric.is_encrypted(value)[source]

Check if the value is encrypted by comparing the first characters of the value to the list of known ciphers.

Args:
  • value: the value to check (AES256CBC$#encrypted_value#)
Returns:
True if the cipher name could be found in the value else False
core.encryption.symmetric.encrypt(plain_text, key, cipher=default_cipher)[source]

Encrypt wrapper function

Args:
  • plain_text: the text to encrypt
  • key: the key to use
  • cipher: override the default cipher
Returns:
encrypted plain_text by key with use of cipher
Raises:
EncryptionException: Cipher is unknown
core.encryption.symmetric.decrypt(cipher_and_encrypted_text, key)[source]

Decrypt wrapper function

the cipher_and_encrypted_text in ciphername$encrypted_text format with use of the key

Args:
  • cipher_and_encrypted_text: encrypted value in ciphername$encrypted_text
  • key: the key to use
Returns:
plain text if key is correct else crap.
Raises:
EncryptionException: Cipher is unknown