title: Using SHA256 method of OpenSSL library in C#
body: <p>I'm coding a wrapper for OpenSSL for C#, and actually everything is working for me now, but i'm having a problem with one method. (I know about OpenSSL.Net, it just doesn't work as I need it and it is missing a lot of methods).</p>
  
  <p>OpenSSL 1.0.1 has SHA256 that I'd like to use. The method itself in the OpenSSL lib C code:</p>
  
  <pre><code>unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
  {
      SHA256_CTX c;
      static unsigned char m[SHA256_DIGEST_LENGTH];
  
      if (md == NULL)
          md = m;
      SHA256_Init(&amp;c);
      SHA256_Update(&amp;c, d, n);
      SHA256_Final(md, &amp;c);
      OPENSSL_cleanse(&amp;c, sizeof(c));
      return (md);
  } 
  </code></pre>
  
  <p>The function itself returns a pointer, and as parameters it needs the input data, the length of the data to hash, and an object for the hash.</p>
  
  <p>I'm wrapping it in the next way:</p>
  
  <pre><code>[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  public extern static IntPtr SHA256(byte[] data, long len, ref byte[] result);
  </code></pre>
  
  <p>I'm trying to run it the next way:</p>
  
  <pre><code>byte[] d = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaa");
  byte[] r = new byte[32];
  IntPtr x = NOpenSSL.Wrapper.SHA256(d, d.Length, ref r);
  Console.WriteLine(d.Length + " " + x + " " + r.Length + " " + Encoding.ASCII.GetString(r));
  </code></pre>
  
  <p>And what i get for result: <strong><em><code>16 1838788744 1</code></em></strong>. The <code>d.Length</code> is 16 OK, I get the x as a pointer OK, the <code>r.Length</code> becomes 1 byte that is impossible.</p>
  
  <p>If I don't <code>ref</code> the <code>byte[]</code> in the method just simply give it to it nothing changes in the variable <code>r</code>.</p>
  
  <p>Can anyone help me please what am i doing wrong?</p>
  
  <p>Thank you for the help in advance!</p>