def test_encrypt_decrypt(self): k1 = new_key() k2 = new_key() k3 = new_key() sender = Encryptor(k1, k2.publickey()) receiver = Encryptor(k2, k1.publickey()) message = b"In the Land of Mordor where the Shadows lie." \ b"One Ring to rule them all, One Ring to find them,\n" \ b"One Ring to bring them all and in the darkness bind them\n" \ b"In the Land of Mordor where the Shadows lie.\n" with print_key_on_error(sender, receiver): cipher_text = sender.encrypt(message) decrypted_message, signature = receiver.decrypt(cipher_text) self.assertEqual(message, decrypted_message) with self.assertRaises(Exception): mangled_cipher_text = cipher_text[2:4] + cipher_text[:2] + cipher_text[4:] receiver.decrypt(mangled_cipher_text)
def dump_config(args): print(Encryptor.new_key().exportKey("PEM").decode("UTF-8"))
import unittest from pynet import Encryptor, Node, PeerDefinition, NodeHooks from pynet.util import to_bytes, _run_node, send_data addr_1 = ("localhost", 54320) # addr_1 = "/tmp/pynet_test_node.sock" addr_2 = ("localhost", 54321) node_pk = Encryptor.new_key() master_encryptor = Encryptor(Encryptor.new_key(), node_pk.publickey()) master_def = PeerDefinition("master", None, master_encryptor.private_key.publickey()) def handle_message(sender, body): return (True, body.upper(), None) class HooksOverride(NodeHooks): def handle_raw_message(self, data): if data == b"register": self.node.write("Registered!", encrypt=False) return (True, None) else: return super().handle_raw_message(data) class TestNode(unittest.TestCase): @classmethod def setUpClass(cls): cls.addr = addr_1
def dump_config(args): node = Node(None, "/tmp/pynet-kvp.sock") node.add_peer(PeerDefinition("first among peers", "127.0.0.1:1337", Encryptor.new_key().publickey())) print(json.dumps(node.get_config(), sort_keys=True, indent=2, separators=(", ", ": ")))
def test_signature(self): k1 = new_key() k2 = new_key() k3 = new_key() sender = Encryptor(k1, k2.publickey()) receiver = Encryptor(k2, k1.publickey()) deceiver = Encryptor(k3, k2.publickey()) message = b"Erised stra ehru oyt ube cafru oyt on wohsi" with print_key_on_error(sender, receiver, deceiver): cipher_text = sender.encrypt(message) decrypted_message, signature = receiver.decrypt(cipher_text) self.assertTrue(receiver.verify_message(decrypted_message, signature)) deceiver_text = deceiver.encrypt(message) decrypted_message, signature = receiver.decrypt(deceiver_text) self.assertFalse(receiver.verify_message(decrypted_message, signature)) _, real_encrypted_message, _ = receiver.split_cipher_text(cipher_text) deceiver_aes, _, deceiver_signature = receiver.split_cipher_text(deceiver_text) cipher_text = deceiver_aes + real_encrypted_message + deceiver_signature decrypted_message, signature = receiver.decrypt(cipher_text) self.assertFalse(receiver.verify_message(decrypted_message, signature))