# Path: auxlib/crypt.py
#     AES_BLOCK_SIZE = AES.block_size
# AES_KEY_SIZE = 32   # 32 byte key size ==> AES-256
# HMAC_SIG_SIZE = hashlib.sha256().digest_size
# def encrypt(secret_key, data):
# def decrypt(secret_key, encryption_key_encrypted, encrypted_data):
# def as_base64(content):
# def from_base64(content):
# def generate_encryption_key():
# def generate_hash_from_secret(secret):
# def aes_encrypt(base64_encryption_key, data):
# def aes_decrypt(base64_encryption_key, base64_data):
# def _pad(s):
# def _unpad(s):
# def _extract_keys(key_str):
# 
# Path: auxlib/crypt.py
#     AES_BLOCK_SIZE = AES.block_size
# AES_KEY_SIZE = 32   # 32 byte key size ==> AES-256
# HMAC_SIG_SIZE = hashlib.sha256().digest_size
# def encrypt(secret_key, data):
# def decrypt(secret_key, encryption_key_encrypted, encrypted_data):
# def as_base64(content):
# def from_base64(content):
# def generate_encryption_key():
# def generate_hash_from_secret(secret):
# def aes_encrypt(base64_encryption_key, data):
# def aes_decrypt(base64_encryption_key, base64_data):
# def _pad(s):
# def _unpad(s):
# def _extract_keys(key_str):


from unittest import TestCase
from auxlib import crypt
from auxlib.crypt import AuthenticationError, encrypt, decrypt
import os
        # hash should always be the same
        self.assertEqual(key_hash, crypt.generate_hash_from_secret(secret))


class TestAESEncryption(TestCase):

    def test_encrypt_and_decrypt_end_to_end(self):
        test_data = u"Mickey & Miννie Μouse"
        encryption_key = crypt.generate_encryption_key()
        encrypted_data = crypt.aes_encrypt(encryption_key, test_data)
        decrypted_data = crypt.aes_decrypt(encryption_key, encrypted_data)
        self.assertEquals(decrypted_data, test_data.encode('UTF-8'))

    def test_invalid_hmac_throws_error(self):
        test_data = u"Mickey & Miννie Μouse"
        encryption_key = crypt.generate_encryption_key()
        encrypted_data = crypt.from_base64(crypt.aes_encrypt(encryption_key, test_data))
        enc_data_only = encrypted_data[:-crypt.HMAC_SIG_SIZE]
        real_hmac_signature = encrypted_data[-crypt.HMAC_SIG_SIZE:]
        fake_hmac_signature = os.urandom(crypt.HMAC_SIG_SIZE)
        self.assertEqual(encrypted_data, enc_data_only + real_hmac_signature)
        self.assertRaises(AuthenticationError, crypt.aes_decrypt, encryption_key,
                          crypt.as_base64(enc_data_only + fake_hmac_signature))
        decrypted_data = crypt.aes_decrypt(encryption_key,
                                           crypt.as_base64(enc_data_only + real_hmac_signature))
        self.assertEquals(decrypted_data, test_data.encode('UTF-8'))

    def test_encrypt_decrypt(self):
        data = ('abcdefg\n' * 5000).encode('UTF-8')
        secret_key = "test_scoobydoobydoo".encode('UTF-8')

# Path: auxlib/crypt.py
#     AES_BLOCK_SIZE = AES.block_size
# AES_KEY_SIZE = 32   # 32 byte key size ==> AES-256
# HMAC_SIG_SIZE = hashlib.sha256().digest_size
# def encrypt(secret_key, data):
# def decrypt(secret_key, encryption_key_encrypted, encrypted_data):
# def as_base64(content):
# def from_base64(content):
# def generate_encryption_key():
# def generate_hash_from_secret(secret):
# def aes_encrypt(base64_encryption_key, data):
# def aes_decrypt(base64_encryption_key, base64_data):
# def _pad(s):
# def _unpad(s):
# def _extract_keys(key_str):
# 
# Path: auxlib/crypt.py
#     AES_BLOCK_SIZE = AES.block_size
# AES_KEY_SIZE = 32   # 32 byte key size ==> AES-256
# HMAC_SIG_SIZE = hashlib.sha256().digest_size
# def encrypt(secret_key, data):
# def decrypt(secret_key, encryption_key_encrypted, encrypted_data):
# def as_base64(content):
# def from_base64(content):
# def generate_encryption_key():
# def generate_hash_from_secret(secret):
# def aes_encrypt(base64_encryption_key, data):
# def aes_decrypt(base64_encryption_key, base64_data):
# def _pad(s):
# def _unpad(s):
# def _extract_keys(key_str):


# Path: tests/test_crypt.py
from unittest import TestCase
from auxlib import crypt
from auxlib.crypt import AuthenticationError, encrypt, decrypt
import os

        # hash should always be the same
        self.assertEqual(key_hash, crypt.generate_hash_from_secret(secret))


class TestAESEncryption(TestCase):

    def test_encrypt_and_decrypt_end_to_end(self):
        test_data = u"Mickey & Miννie Μouse"
        encryption_key = crypt.generate_encryption_key()
        encrypted_data = crypt.aes_encrypt(encryption_key, test_data)
        decrypted_data = crypt.aes_decrypt(encryption_key, encrypted_data)
        self.assertEquals(decrypted_data, test_data.encode('UTF-8'))

    def test_invalid_hmac_throws_error(self):
        test_data = u"Mickey & Miννie Μouse"
        encryption_key = crypt.generate_encryption_key()
        encrypted_data = crypt.from_base64(crypt.aes_encrypt(encryption_key, test_data))
        enc_data_only = encrypted_data[:-crypt.HMAC_SIG_SIZE]
        real_hmac_signature = encrypted_data[-crypt.HMAC_SIG_SIZE:]
        fake_hmac_signature = os.urandom(crypt.HMAC_SIG_SIZE)
        self.assertEqual(encrypted_data, enc_data_only + real_hmac_signature)
        self.assertRaises(AuthenticationError, crypt.aes_decrypt, encryption_key,
                          crypt.as_base64(enc_data_only + fake_hmac_signature))
        decrypted_data = crypt.aes_decrypt(encryption_key,
                                           crypt.as_base64(enc_data_only + real_hmac_signature))
        self.assertEquals(decrypted_data, test_data.encode('UTF-8'))

    def test_encrypt_decrypt(self):
        data = ('abcdefg\n' * 5000).encode('UTF-8')
        secret_key = "test_scoobydoobydoo".encode('UTF-8')

        encryption_key_encrypted, encrypted_data = encrypt(secret_key, data)