def prepare_privmsg(self, nick, cmd, message, mc=None): # should we encrypt? box, encrypt = self.get_encryption_box(cmd, nick) if encrypt: if not box: log.debug('error, dont have encryption box object for ' + nick + ', dropping message') return message = encrypt_encode(message.encode('ascii'), box) #Anti-replay measure: append the message channel identifier #to the signature; this prevents cross-channel replay but NOT #same-channel replay (in case of snooper after dropped connection #on this channel). if mc is None: if nick in self.active_channels: hostid = self.active_channels[nick].hostid else: log.info("Failed to send message to: " + str(nick) + \ "; cannot find on any message channel.") return else: hostid = mc.hostid msg_to_be_signed = message + str(hostid) self.daemon.request_signed_message(nick, cmd, message, msg_to_be_signed, hostid)
def test_enc_wrapper(alice_bob_boxes, ab_message, ba_message, num_iterations): alice_box, bob_box = alice_bob_boxes for i in range(num_iterations): ab_message = ''.join( random.choice(string.ascii_letters) for x in range(100)) if ab_message == 'rand' else ab_message ba_message = ''.join( random.choice(string.ascii_letters) for x in range(100)) if ba_message == 'rand' else ba_message otw_amsg = alice_box.encrypt(ab_message) bob_ptext = bob_box.decrypt(otw_amsg) assert bob_ptext == ab_message, "Encryption test: FAILED. Alice sent: %s, Bob received: " % ( ab_message, bob_ptext) otw_bmsg = bob_box.encrypt(ba_message) alice_ptext = alice_box.decrypt(otw_bmsg) assert alice_ptext == ba_message, "Encryption test: FAILED. Bob sent: %s, Alice received: " % ( ba_message, alice_ptext) assert decode_decrypt(encrypt_encode(ab_message, bob_box), bob_box) == ab_message