开发者

What is the C++ equivalent of PHP's hash_hmac function?

I'm porting a PHP application to C++. The PHP application is using this function:

hash_hmac — Generate a keyed hash value using the HMAC method

If I have this code, what is it actually doing?

$sStr = hash_hmac ('sha256', $mydata,$mykey, $raw = true)

I know it encrypts some 开发者_JS百科data using sha256 and my key, but how can I perform this in C++?

I've found the hmac and sha2 libraries, but am not sure if they are what I need.


I would consider looking into OpenSSL, a portable and complete cryptographic library (despite its name, it doesn't just do SSL). It has an HMAC library which you can surely wrap to get similar function.

Here's an example on how to use OpenSSL' HMAC library, taken from another question on StackOverflow (annotations mine):

  // Initialize HMAC object.
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);

  // Set HMAC key.
HMAC_Init_ex(&ctx, key, 16, EVP_sha256(), NULL);

  // May be called repeatedly to insert all your data.
HMAC_Update(&ctx, data, 8);

  // Finish HMAC computation and fetch result.
HMAC_Final(&ctx, result, &result_len);

  // Done with HMAC object.
HMAC_CTX_cleanup(&ctx);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜