"""Encryption module that uses pyca/cryptography"""

import os
import json

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import (
    Cipher, algorithms, modes
)


def aesEncrypt(data, key):
    # Generate a random 96-bit IV.
    iv = os.urandom(12)

    # Construct an AES-GCM Cipher object with the given key and a
    # randomly generated IV.
    encryptor = Cipher(
        algorithms.AES(key),
        modes.GCM(iv),
        backend=default_backend()
    ).encryptor()

    # associated_data will be authenticated but not encrypted,
    # it must also be passed in on decryption.
    encryptor.authenticate_additional_data("")

    # Encrypt the plaintext and get the associated ciphertext.
    # GCM does not require padding.
    ciphertext = encryptor.update(data) + encryptor.finalize()

    return iv + encryptor.tag + ciphertext


def aesDecrypt(data, key):
    iv = data[:12]
    tag = data[12:28]
    ciphertext = data[28:]

    # Construct a Cipher object, with the key, iv, and additionally the
    # GCM tag used for authenticating the message.
    decryptor = Cipher(
        algorithms.AES(key),
        modes.GCM(iv, tag),
        backend=default_backend()
    ).decryptor()

    # We put associated_data back in or the tag will fail to verify
    # when we finalize the decryptor.
    decryptor.authenticate_additional_data("")

    # Decryption gets us the authenticated plaintext.
    # If the tag does not match an InvalidTag exception will be raised.
    return decryptor.update(ciphertext) + decryptor.finalize()


has_aes = True

def getKeyLength():
    return 32

@@ -528,166 +528,8 @@
 ()%0A%0A
-    # associated_data will be authenticated but not encrypted,%0A    # it must also be passed in on decryption.%0A    encryptor.authenticate_additional_data(%22%22)%0A%0A
     
@@ -1089,162 +1089,8 @@
 ()%0A%0A
-    # We put associated_data back in or the tag will fail to verify%0A    # when we finalize the decryptor.%0A    decryptor.authenticate_additional_data(%22%22)%0A%0A
     

Remove associated data