title: Serializing part of a message with Protobuf
body: <p>I am trying to serialize/deserialize a sub message using Protobuf.
  The reasons for this is signing. I want to be able to sign part of my message. To be able to do that, I need to canonicalize it somehow.</p>
  
  <p>If it matters, I use protbuf 3.0.0-alpha (With the proto2 language) with Python3.4.</p>
  
  <p>Example file: testp.proto</p>
  
  <pre><code>package my_package;
  
  message my_mess {
    message data {
      optional uint64 x = 1;
      optional uint64 y = 2;
      optional uint64 z = 3;
    }
      optional bytes signature = 4;
  }
  </code></pre>
  
  <p>In this example, I want to sign the data part of the message. Therefore I want to serialize only my_mess.data, sign it, put the signature into my_mess.signature, and then serialize the full message my_mess.</p>
  
  <p>Compiling testp.proto:</p>
  
  <pre><code>$ protoc -I=. --python_out=. testp.proto 
  [libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
  </code></pre>
  
  <p>I noticed that mm.data have the methods SerializeToString and SerializePartialToString.
  However, it seems like it is not possible to run them directly. Here are my attempts:</p>
  
  <pre><code>$ ipython
  
  In [1]: import testp_pb2
  In [2]: mm = testp_pb2.my_mess()
  In [3]: mm.data.x = 1
  In [4]: mm.data.y = 2
  In [5]: mm.data.z = 3
  In [6]: mm.data.SerializeToString()
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-6-faa8e25906ca&gt; in &lt;module&gt;()
  ----&gt; 1 mm.data.SerializeToString()
  
  TypeError: SerializeToString() missing 1 required positional argument: 'self'
  
  In [7]: mm.data.SerializePartialToString()
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-7-4b2c1ff0b1c9&gt; in &lt;module&gt;()
  ----&gt; 1 mm.data.SerializePartialToString()
  
  TypeError: SerializePartialToString() missing 1 required positional argument: 'self'
  </code></pre>
  
  <p>I was wondering if there is some simple solution I'm missing. I am open to other suggestions, if you have an idea of signing just a part of the message in an elegant way.</p>
  
  <p>Thank you for your help.</p>