Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I encode a string using HMAC SHA256 algorithm? I went through OpenSSL library but didn't find anything valuable. Your suggestions?
HMAC and SHA256 are separate components in OpenSSL, you'll need to glue them together yourself. (Note that this uses the shorthand methods for doing everything in one shot with monolithic buffers; incremental processing is more complex.)
#include <openssl/evp.h>
#include <openssl/hmac.h>
unsigned char* hmac_sha256(const void *key, int keylen,
const unsigned char *data, int datalen,
unsigned char *result, unsigned int* resultlen)
{
return HMAC(EVP_sha256(), key, keylen, data, datalen, result, resultlen);
}
Even if your input is a string, the result is an arbitrary byte array; if that too needs to be a string then you'll have to apply some other transformation like hexadecimal expansion, Base64 or whatever suits your application.