Encoding a string in HMAC SHA256 [closed]

2020-07-21 05:14发布

How can I encode a string using HMAC SHA256 algorithm? I went through OpenSSL library but didn't find anything valuable. Your suggestions?

1条回答
神经病院院长
2楼-- · 2020-07-21 05:46

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.

查看更多
登录 后发表回答