0

The code i wrote somehow does not work properly. When using the same key and cipher with an online tool, the hmac is different.

My code is:

    EVP_MD_CTX* mdctx = NULL;
    const EVP_MD* md = NULL;
    EVP_PKEY *pkey = NULL;
    
    unsigned char md_value[EVP_MAX_MD_SIZE];
    size_t md_len = 0;

    mdctx = EVP_MD_CTX_create();
    md = EVP_get_digestbyname("SHA256");
    const unsigned char  a[] = "qwertzuiopqwertzuiopqw";
    pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, a, strlen("qwertzuiopqwertzuiopqw"));
    EVP_DigestSignInit(mdctx, NULL, md, NULL, pkey);
   EVP_DigestSignUpdate(mdctx, "Hallo", sizeof("Hallo")); // iv
    EVP_DigestSignFinal(mdctx, md_value, &md_len);

The hmac calculated by my code is : eadfb51c9fb3f3fcd5741006861d04bc0d695347db4bfb9e04e954e17583c3c5.

The hmac calculated by the online-tool is: a9e0f9acc1452bdb796556c3c64e29d7f7ab47a59a179ec918c61894eefeba26.

What am i doing wrong?

root
  • 35
  • 5
  • 1
    What are you choosing as the message digest algorithm in the online tool? – mnistic Sep 15 '20 at 02:12
  • 2
    Your code is not a reproducible example, but if I complete it in what seems the obvious way I get ffdf6f4cbbe7defb569db9d9a82250e5d7f32d41e68971f284ac39a456673d41 not the value you post. This differs from your website value, which is indeed the HmacSHA256 of the letters `H a l l o`, in that your code with my completion computes the HmacSHA256 of those letters PLUS THE TERMINATING NULL BYTE which can be written explicitly `'\0'` in C (and some other) syntax. – dave_thompson_085 Sep 15 '20 at 05:17

0 Answers0