def test_encode_random_bytes(self): key = encoded_random_bytes(num_bytes=16) self.assertEqual(len(decode_bytes(key)), 16) key = encoded_random_bytes(num_bytes=24) self.assertEqual(len(decode_bytes(key)), 24) key = encoded_random_bytes(num_bytes=32) self.assertEqual(len(decode_bytes(key)), 32)
def setUp(self): self.app = CompanyApp(uri="sqlite://", cipher_key=encoded_random_bytes(num_bytes=32), persist_event_type=(Company.Event, Person.Event)) self.jim_id = self.app.involve_person(title="Mr", name="James Holden", address="Earth", date_of_birth="08/1987") self.naomi_id = self.app.involve_person(title="Miss", name="Naomi Nagata", address="The Belt", date_of_birth="03/1992") self.amos_id = self.app.involve_person(title="Mr", name="Amos Burton", address="Earth", date_of_birth="02/1991") self.alex_id = self.app.involve_person(title="Mr", name="Alex Kamal", address="Mars", date_of_birth="08/1985") self.company_id = self.app.prepare_new_company( name="Rocinante Limited", address="Space", sic_code=27151, initial_directors_ids=[self.jim_id, self.naomi_id])
def test_encrypt_mode_gcm(self): from eventsourcing.utils.cipher.aes import AESCipher from eventsourcing.utils.random import encoded_random_bytes, decode_bytes # Unicode string representing 256 random bits encoded with Base64. cipher_key = encoded_random_bytes(num_bytes=32) # Construct AES cipher. cipher = AESCipher(cipher_key=decode_bytes(cipher_key)) # Encrypt some plaintext. ciphertext = cipher.encrypt(b"plaintext") self.assertNotEqual(ciphertext, "plaintext") # Decrypt some ciphertext. plaintext = cipher.decrypt(ciphertext) self.assertEqual(plaintext, b"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 = b"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)
number_of_shares=number_of_shares, nominal_value_per_share=nominal_value_per_share, price_paid_per_share=price_paid_per_share, votes_per_share=1, entitled_to_dividends=True, entitled_to_capital=True, redeemable=False ) company.__save__() person.record_shareholding(shareholding) person.__save__() if __name__ == "__main__": # For using the Python shell from eventsourcing.utils.random import encoded_random_bytes cipher_key = encoded_random_bytes(32) app = CompanyApp(persist_event_type=(Company.Event, Person.Event), uri="sqlite:///:memory:", cipher_key=cipher_key) jim_id = app.involve_person(title="Mr", name="James Holden", address="Earth", date_of_birth = "08/1987") amos_id = app.involve_person(title="Mr", name="Amos Burton", address="Earth", date_of_birth = "02/1991") naomi_id = app.involve_person(title="Miss", name="Naomi Nagata", address="The Belt", date_of_birth = "03/1992") alex_id = app.involve_person(title="Mr", name="Alex Kamal", address="Mars", date_of_birth = "08/1985") company_id = app.prepare_new_company(name="Rocinante Limited", address="Space", sic_code=27151, initial_directors_ids=[jim_id, naomi_id]) for person_id in [jim_id, amos_id, naomi_id, alex_id]: app.issue_ordinary_shares(in_company_id=company_id, to_person_id=person_id, number_of_shares=2500, nominal_value_per_share=Decimal("0.0001"), price_paid_per_share=Decimal("0.0001")) company = app.repository[company_id] for event in app.notification_log.get_items(start=0, stop=20): print(event, "\n")
def construct_concrete_application(self): return self.application_class.mixin(self.infrastructure_class)( cipher_key=encoded_random_bytes(16), persist_event_type=DomainEvent)
def get_application(self): return self.application_class(cipher_key=encoded_random_bytes(16), persist_event_type=DomainEvent)