title: MD5 HMAC With OpenSSL
body: <p>I was trying to generate MD5 HMAC with OpenSSL &amp; most of the code is borrowed.  The hmac being generate is incorrect:</p>
  
  <pre><code>#include &lt;openssl/hmac.h&gt;
  #include &lt;openssl/evp.h&gt;
  #include &lt;syslog.h&gt;
  #include &lt;string.h&gt;
  
  #include &lt;openssl/engine.h&gt;
  #include &lt;openssl/hmac.h&gt;
  #include &lt;openssl/evp.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main() 
  {
    unsigned char* key = (unsigned char*) "2012121220121212201212122012121220121212201212122012121220121212";
    unsigned char* data = (unsigned char*) "johndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoe";
    unsigned char* expected = (unsigned char*) "abcd1d87dca34f334786307d0da4fcbd";
    unsigned char* result;
    // unsigned int result_len = 16;
    unsigned int result_len = 16;
    int i;
    static char res_hexstring[32];
  
    // result = HMAC(EVP_sha256(), key, 4, data, 28, NULL, NULL);
    result = HMAC(EVP_md5(), key, 32, data, 28, NULL, NULL);
    for (i = 0; i &lt; result_len; i++) {
      sprintf(&amp;(res_hexstring[i * 2]), "%02x", result[i]);
    }
  
    if (strcmp((char*) res_hexstring, (char*) expected) == 0) {
      printf("Test ok, result length %d\n", result_len);
    } else {
      printf("Got %s instead of %s\n", res_hexstring, expected);
    }
  }
  </code></pre>
  
  <p>The hash being produced is incorrect. I would appreciate some feedback or someone pointing me in the right direction.</p>