def test_encrypt_mode_gcm(self):
        from eventsourcing.utils.cipher.aes import AESCipher
        from eventsourcing.utils.random import encode_random_bytes, decode_random_bytes

        # Unicode string representing 256 random bits encoded with Base64.
        cipher_key = encode_random_bytes(num_bytes=32)

        # Construct AES cipher.
        cipher = AESCipher(cipher_key=decode_random_bytes(cipher_key))

        # Encrypt some plaintext.
        ciphertext = cipher.encrypt('plaintext')
        self.assertNotEqual(ciphertext, 'plaintext')

        # Decrypt some ciphertext.
        plaintext = cipher.decrypt(ciphertext)
        self.assertEqual(plaintext, 'plaintext')

        # Check DataIntegrityError is raised (broken Base64 padding).
        with self.assertRaises(DataIntegrityError):
            damaged = ciphertext[:-1]
            cipher.decrypt(damaged)

        # Check DataIntegrityError is raised (MAC check fails).
        with self.assertRaises(DataIntegrityError):
            damaged = 'a' + ciphertext[:-1]
            cipher.decrypt(damaged)
Example #2
0
"""
This is part of the code accompanying this article: 
https://www.eventsorcery.com/python-eventsourcing-tutorial-part-2-event-store/ 
"""

from eventsourcing.infrastructure.sqlalchemy.datastore import SQLAlchemySettings, SQLAlchemyDatastore
from eventsourcing.infrastructure.sqlalchemy.records import StoredEventRecord
from eventsourcing.infrastructure.sqlalchemy.manager import SQLAlchemyRecordManager
from eventsourcing.infrastructure.sequenceditem import StoredEvent
from eventsourcing.infrastructure.sequenceditemmapper import SequencedItemMapper
from eventsourcing.infrastructure.eventstore import EventStore
from eventsourcing.utils.cipher.aes import AESCipher
from eventsourcing.utils.random import encode_random_bytes, decode_bytes

cipher_key = encode_random_bytes(num_bytes=32)
cipher = AESCipher(cipher_key=decode_bytes(cipher_key))


def construct_sqlalchemy_db(uri="sqlite://") -> SQLAlchemyDatastore:
    db = SQLAlchemyDatastore(settings=SQLAlchemySettings(uri),
                             tables=(StoredEventRecord, ))
    db.setup_connection()
    db.drop_tables()
    db.setup_tables()
    return db


def create_event_store(db) -> EventStore:
    record_manager = SQLAlchemyRecordManager(session=db.session,
                                             sequenced_item_class=StoredEvent,
                                             record_class=StoredEventRecord,
Example #3
0
 def construct_cipher(self,
                      cipher_key_str: Optional[str]) -> Optional[AESCipher]:
     cipher_key_bytes = decode_bytes(cipher_key_str
                                     or os.getenv("CIPHER_KEY", "") or "")
     return AESCipher(cipher_key_bytes) if cipher_key_bytes else None
Example #4
0
 def setup_cipher(self, cipher_key):
     cipher_key = decode_random_bytes(cipher_key
                                      or os.getenv('CIPHER_KEY', ''))
     self.cipher = AESCipher(cipher_key) if cipher_key else None
Example #5
0
 def construct_cipher(self, cipher_key):
     cipher_key = decode_bytes(cipher_key or os.getenv('CIPHER_KEY', ''))
     self.cipher = AESCipher(cipher_key) if cipher_key else None
Example #6
0
 def construct_cipher(self):
     return AESCipher(cipher_key=b'0123456789abcdef')