'Accepts a list of messages and encodes for transmission.
This function signs and then encrypts the payload.
Args:
message_list: A MessageList rdfvalue containing a list of
GrrMessages.
result: A ClientCommunication rdfvalue which will be filled in.
destination: The CN of the remote system this should go to.
timestamp: A timestamp to use for the signed messages. If None - use the
current time.
api_version: The api version which this should be encoded in.
Returns:
A nonce (based on time) which is inserted to the encrypted payload. The
client can verify that the server is able to decrypt the message and
return the nonce.
Raises:
RuntimeError: If we do not support this api version.'

def EncodeMessages(self, message_list, result, destination=None, timestamp=None, api_version=3):

    if (api_version not in [3]):
        raise RuntimeError(('Unsupported    api    version:    %s,    expected    3.' % api_version))
    if (destination is None):
        destination = self.server_name
        cipher = self._GetServerCipher()
    else:
        remote_public_key = self._GetRemotePublicKey(destination)
        cipher = Cipher(self.common_name, self.private_key, remote_public_key)
    if (timestamp is None):
        self.timestamp = timestamp = long((time.time() * 1000000))
    signed_message_list = rdf_flows.SignedMessageList(timestamp=timestamp)
    self.EncodeMessageList(message_list, signed_message_list)
    result.encrypted_cipher_metadata = cipher.encrypted_cipher_metadata
    result.encrypted_cipher = cipher.encrypted_cipher
    serialized_message_list = signed_message_list.SerializeToString()
    (result.packet_iv, result.encrypted) = cipher.Encrypt(serialized_message_list)
    result.hmac = cipher.HMAC(result.encrypted)
    result.full_hmac = cipher.HMAC(result.encrypted, result.encrypted_cipher, result.encrypted_cipher_metadata, result.packet_iv.SerializeToString(), struct.pack('<I', api_version))
    result.api_version = api_version
    if isinstance(result, rdfvalue.RDFValue):
        result.num_messages = len(message_list)
    return timestamp
