Ejemplo n.º 1
0
 def assertSamePayload(self, msg1, msg2):
     dump1 = serializer.dumps(msg1.slots())
     dump2 = serializer.dumps(msg2.slots())
     return self.assertEqual(dump1,
                             dump2,
                             msg="Message payload differs: \n\n%s\n\n%s" %
                             (msg1.slots(), msg2.slots()))
Ejemplo n.º 2
0
    def serialize(self, sign_as: bytes = None, encrypt_func=None):
        """ Return serialized message
        :return str: serialized message """

        if sign_as and self.sig:
            # If you wish to overwrite signature,
            # first set it to None explicitly
            raise exceptions.SignatureAlreadyExists()

        self.encrypted = bool(self.ENCRYPT and encrypt_func)
        payload = serializer.dumps(self.slots())

        # When nesting one message inside another it's important
        # not to overwrite original signature.
        if self.sig is None:
            if sign_as and self.SIGN:
                self.sign_message(private_key=sign_as,
                                  msg_hash=self.get_short_hash(payload))
            else:
                self._fake_sign()

        if self.encrypted:
            payload = encrypt_func(payload)

        return (self.serialize_header() + self.sig + payload)
Ejemplo n.º 3
0
    def get_short_hash(self, payload=None) -> bytes:
        """Return short message representation for signature
        :return bytes: sha1(TYPE, timestamp, payload)
        """
        if payload is None:
            payload = serializer.dumps(self.slots())
        sha = hashlib.sha1()

        # We can't use self.serialize_header() because it includes
        # self.encrypted. And nested messages are decrypted, but they
        # still need to have a valid signature.
        # SEE: test_serializer.MessageTestCase.test_message_sig()
        hash_header = serializer.dumps([
            self.TYPE,
            self.timestamp,
        ])
        sha.update(hash_header)
        sha.update(payload or b'')
        return sha.digest()
Ejemplo n.º 4
0
 def equal_after_processing(self, o):
     s = serializer.dumps(o)
     o2 = serializer.loads(s)
     self.assertEqual(o, o2)
Ejemplo n.º 5
0
 def test_disconnect_reason(self):
     r = message.Disconnect.REASON.TooManyPeers
     s = serializer.dumps(r)
     r2 = serializer.loads(s)
     self.assertIs(r, r2)