Пример #1
0
    def test_encode_random_bytes(self):
        key = encode_random_bytes(num_bytes=16)
        self.assertEqual(len(decode_bytes(key)), 16)

        key = encode_random_bytes(num_bytes=24)
        self.assertEqual(len(decode_bytes(key)), 24)

        key = encode_random_bytes(num_bytes=32)
        self.assertEqual(len(decode_bytes(key)), 32)
Пример #2
0
    def test_encrypt_mode_gcm(self):
        from eventsourcing.utils.cipher.aes import AESCipher
        from eventsourcing.utils.random import encode_random_bytes, decode_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_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)

        # Check DataIntegrityError is raised (nonce too short).
        with self.assertRaises(DataIntegrityError):
            damaged = ciphertext[:0]
            cipher.decrypt(damaged)

        # Check DataIntegrityError is raised (tag too short).
        with self.assertRaises(DataIntegrityError):
            damaged = ciphertext[:20]
            cipher.decrypt(damaged)
Пример #3
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,
Пример #4
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
Пример #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