title: Using JAXB with signatures, encryption and encoding
body: <p>Recently we've been tasked with coming up with a XML communication specification for our products. A few of my coworkers have high opinions of JAXB for marshalling and unmarshalling XML docs. I've spent some time playing around with it and I understand where they are coming from. It makes life simple for simple XML docs.</p>
  
  <p>Now to take it up a notch. One of the things that I would like to see in our communication model "built in" signature validation for people who use it after me. One of the problems I'm running into is that to validate a signature I need to treat the corresponding XML as bytes. So let's take this example...</p>
  
  <pre><code>&lt;topLevel&gt;
      &lt;sensitiveData encoding="UTF8"&gt;
          &lt;creditCard&gt;
              &lt;number&gt;1234-1234-1234-1234&lt;/number&gt;
              &lt;expDate&gt;Oct 2020&lt;/expDate&gt;
          &lt;/creditCard&gt;
      &lt;/sensitiveData&gt;
      &lt;signatureOfSensitiveData algorithm="SHA1WithRSA"&gt;VGhpc0lzQVNpZ25hdHVyZQ==&lt;/signatureOfSensitiveData&gt;
  &lt;/topLevel&gt;
  </code></pre>
  
  <p>Edit: I am not actually passing credit card data. Just an example here.</p>
  
  <p>What would be great is if I could get the <code>byte[]</code> (determined by the encoding) representation of everything inside of the "sensitiveData" tag. I wouldn't even mind having to call "unmarshall" again on that <code>byte[]</code>.</p>
  
  <p>This also opens up other doors for us. We could actually introduce "compression" and "encryption" attributes into elements. If we could treat them as a <code>byte[]</code> we could then inflate and decrypt them and then pass them on to be unmarshalled again.</p>
  
  <p>Side note: I think this works if you base64 encode the XML and then include it in an element. But that then forces us to base64 even simple documents and introduce some unnecessary bloat into our messages.</p>
  
  <p>Any ideas for solutions to this? My hope is that I'm just missing something basic in JAXB and it will be a breeze after I get that.</p>
  
  <p>Thanks!</p>