title: C# Calculate and verify SHA256 value for SEPA (XML) paymentfile
body: <p>i am struggling with the same issue like Markus Dreyer:
  <a href="https://stackoverflow.com/questions/18254277/c-sharp-calculate-sha256-value-for-sepa-xml-paymentfile">C# Calculate SHA256 value for SEPA (XML) paymentfile</a></p>
  
  <p>According to the DFÜ Agreement i have to calculate a sha256 hash value:</p>
  
  <ul>
  <li>The hash value is created using the entire contained document, including the opening and closing  tag.</li>
  <li>The document is canonicalized according to Canonical XML, version 1.0. (<a href="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" rel="nofollow noreferrer">http://www.w3.org/TR/2001/REC-xml-c14n-20010315</a>).</li>
  <li>In the case of included documents, the canonisation has also to be executed accord-ing to the main document.</li>
  <li>SHA-256 is used as hash algorithm.</li>
  </ul>
  
  <p>This is a sample valid xml File ( exported from an financial tool ) :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;conxml xmlns="urn:conxml:xsd:container.nnn.002" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:conxml:xsd:container.nnn.002 container.nnn.002.xsd"&gt;
    &lt;CreDtTm&gt;2013-08-27T07:20:25Z&lt;/CreDtTm&gt;
    &lt;MsgPain001&gt;
      &lt;HashValue&gt;33E579FE7A9AF6C32C100E8578EBD63E54A2DF47C6849F7A4BC8BEA9E2794197&lt;/HashValue&gt;
      &lt;HashAlgorithm&gt;SHA256&lt;/HashAlgorithm&gt;
      &lt;Document xmlns="urn:swift:xsd:$pain.001.002.02"&gt;
        &lt;pain.001.001.02&gt;
          &lt;GrpHdr&gt;
            &lt;MsgId&gt;D005201308270920191&lt;/MsgId&gt;
            &lt;CreDtTm&gt;2013-08-27T07:20:19Z&lt;/CreDtTm&gt;
            &lt;BtchBookg&gt;true&lt;/BtchBookg&gt;
            &lt;NbOfTxs&gt;1&lt;/NbOfTxs&gt;
            &lt;CtrlSum&gt;0.50&lt;/CtrlSum&gt;
            &lt;Grpg&gt;MIXD&lt;/Grpg&gt;
            &lt;InitgPty&gt;
              &lt;Nm&gt;Test&lt;/Nm&gt;
            &lt;/InitgPty&gt;
          &lt;/GrpHdr&gt;
          &lt;PmtInf&gt;
            &lt;PmtInfId&gt;D005201308270920191&lt;/PmtInfId&gt;
            &lt;PmtMtd&gt;TRF&lt;/PmtMtd&gt;
            &lt;PmtTpInf&gt;
              &lt;SvcLvl&gt;
                &lt;Cd&gt;SEPA&lt;/Cd&gt;
              &lt;/SvcLvl&gt;
            &lt;/PmtTpInf&gt;
            &lt;ReqdExctnDt&gt;2013-08-27&lt;/ReqdExctnDt&gt;
            &lt;Dbtr&gt;
              &lt;Nm&gt;Test&lt;/Nm&gt;
            &lt;/Dbtr&gt;
            &lt;DbtrAcct&gt;
              &lt;Id&gt;
                &lt;IBAN&gt;DE76200700000888888888&lt;/IBAN&gt;
              &lt;/Id&gt;
            &lt;/DbtrAcct&gt;
            &lt;DbtrAgt&gt;
              &lt;FinInstnId&gt;
                &lt;BIC&gt;DEUTDEHHXXX&lt;/BIC&gt;
              &lt;/FinInstnId&gt;
            &lt;/DbtrAgt&gt;
            &lt;ChrgBr&gt;SLEV&lt;/ChrgBr&gt;
            &lt;CdtTrfTxInf&gt;
              &lt;PmtId&gt;
                &lt;EndToEndId&gt;NOTPROVIDED&lt;/EndToEndId&gt;
              &lt;/PmtId&gt;
              &lt;Amt&gt;
                &lt;InstdAmt Ccy="EUR"&gt;0.50&lt;/InstdAmt&gt;
              &lt;/Amt&gt;
              &lt;CdtrAgt&gt;
                &lt;FinInstnId&gt;
                  &lt;BIC&gt;DEUTDEHHXXX&lt;/BIC&gt;
                &lt;/FinInstnId&gt;
              &lt;/CdtrAgt&gt;
              &lt;Cdtr&gt;
                &lt;Nm&gt;Erwin Mustermann&lt;/Nm&gt;
              &lt;/Cdtr&gt;
              &lt;CdtrAcct&gt;
                &lt;Id&gt;
                  &lt;IBAN&gt;DE09200700000123456789&lt;/IBAN&gt;
                &lt;/Id&gt;
              &lt;/CdtrAcct&gt;
              &lt;RmtInf&gt;
                &lt;Ustrd&gt;Sepa Test Gutschrift&lt;/Ustrd&gt;
              &lt;/RmtInf&gt;
            &lt;/CdtTrfTxInf&gt;
          &lt;/PmtInf&gt;
        &lt;/pain.001.001.02&gt;
      &lt;/Document&gt;
    &lt;/MsgPain001&gt;
  &lt;/conxml&gt;
  </code></pre>
  
  <p>According to the Solution from Markus Dreyer, here is my code:</p>
  
  <pre><code>System.Text.UTF8Encoding enc = new UTF8Encoding(false);
  
  XmlDocument doc = new XmlDocument();
  doc.PreserveWhitespace = true;
  doc.Load(@"path to file");
  XmlNodeList list = doc.GetElementsByTagName("Document");
  String s = list.Item(0).OuterXml;
  
  
  MemoryStream msIn = new MemoryStream(enc.GetBytes(s));
  
  XmlDsigC14NTransform t = new XmlDsigC14NTransform(true);
  t.LoadInput(msIn);
  MemoryStream ms = new MemoryStream();
  ms = (MemoryStream)t.GetOutput(typeof(MemoryStream));
  
  
  byte[] digest = t.GetDigestedOutput(new SHA256Managed());
  String result = BitConverter.ToString(digest).Replace("-", String.Empty);
  </code></pre>
  
  <p>In my calculation i got the value: 
  55B2597B0688AB1A19760B542AA70AEF4F980D7BC9D6EBCF2B741F6299C661D3
  but expected is the value from the file:
  33E579FE7A9AF6C32C100E8578EBD63E54A2DF47C6849F7A4BC8BEA9E2794197</p>
  
  <p>Have any of you an idea, what i am missing?</p>