def encode(self, is_originating, uri, args=None, kwargs=None): """ Encrypt the given WAMP URI, args and kwargs into an EncodedPayload instance, or None if the URI should not be encrypted. """ assert (type(is_originating) == bool) assert (type(uri) == six.text_type) assert (args is None or type(args) in (list, tuple)) assert (kwargs is None or type(kwargs) == dict) box = self._get_box(is_originating, uri) if not box: # if we didn't find a crypto box, then return None, which # signals that the payload travel unencrypted (normal) return None payload = {u'uri': uri, u'args': args, u'kwargs': kwargs} nonce = random(Box.NONCE_SIZE) payload_ser = _json_dumps(payload).encode('utf8') payload_encr = box.encrypt(payload_ser, nonce, encoder=RawEncoder) # above returns an instance of http://pynacl.readthedocs.io/en/latest/utils/#nacl.utils.EncryptedMessage # which is a bytes _subclass_! hence we apply bytes() to get at the underlying plain # bytes "scalar", which is the concatenation of `payload_encr.nonce + payload_encr.ciphertext` payload_bytes = bytes(payload_encr) payload_key = None return EncodedPayload(payload_bytes, u'cryptobox', u'json', enc_key=payload_key)
def encode(self, is_originating, uri, args=None, kwargs=None): """ Encrypt the given WAMP URI, args and kwargs into an EncodedPayload instance, or None if the URI should not be encrypted. """ assert(type(is_originating) == bool) assert(type(uri) == six.text_type) assert(args is None or type(args) in (list, tuple)) assert(kwargs is None or type(kwargs) == dict) box = self._get_box(is_originating, uri) if not box: # if we didn't find a crypto box, then return None, which # signals that the payload travel unencrypted (normal) return None payload = { u'uri': uri, u'args': args, u'kwargs': kwargs } nonce = random(Box.NONCE_SIZE) payload_ser = _json_dumps(payload).encode('utf8') payload_encr = box.encrypt(payload_ser, nonce, encoder=RawEncoder) # above returns an instance of http://pynacl.readthedocs.io/en/latest/utils/#nacl.utils.EncryptedMessage # which is a bytes _subclass_! hence we apply bytes() to get at the underlying plain # bytes "scalar", which is the concatenation of `payload_encr.nonce + payload_encr.ciphertext` payload_bytes = bytes(payload_encr) payload_key = None return EncodedPayload(payload_bytes, u'cryptobox', u'json', enc_key=payload_key)