2018-11-07 21:26:32 +01:00
|
|
|
/*
|
|
|
|
* IXHMac.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
|
|
|
*/
|
2019-06-03 05:46:20 +02:00
|
|
|
|
2018-11-07 21:26:32 +01:00
|
|
|
#include "IXHMac.h"
|
2018-11-13 02:56:59 +01:00
|
|
|
#include "IXBase64.h"
|
2018-11-07 21:26:32 +01:00
|
|
|
|
2019-06-03 05:46:20 +02:00
|
|
|
#if defined(IXWEBSOCKET_USE_MBED_TLS)
|
|
|
|
# include <mbedtls/md.h>
|
|
|
|
#elif defined(__APPLE__)
|
2019-04-09 06:52:20 +02:00
|
|
|
# include <CommonCrypto/CommonHMAC.h>
|
|
|
|
#else
|
|
|
|
# include <openssl/hmac.h>
|
|
|
|
#endif
|
2018-11-07 21:26:32 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
std::string hmac(const std::string& data, const std::string& key)
|
|
|
|
{
|
|
|
|
constexpr size_t hashSize = 16;
|
|
|
|
unsigned char hash[hashSize];
|
|
|
|
|
2019-06-03 05:46:20 +02:00
|
|
|
#if defined(IXWEBSOCKET_USE_MBED_TLS)
|
|
|
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_MD5),
|
|
|
|
(unsigned char *) key.c_str(), key.size(),
|
|
|
|
(unsigned char *) data.c_str(), data.size(),
|
|
|
|
(unsigned char *) &hash);
|
|
|
|
#elif defined(__APPLE__)
|
2019-04-09 06:52:20 +02:00
|
|
|
CCHmac(kCCHmacAlgMD5,
|
|
|
|
key.c_str(), key.size(),
|
|
|
|
data.c_str(), data.size(),
|
|
|
|
&hash);
|
|
|
|
#else
|
2018-11-07 21:26:32 +01:00
|
|
|
HMAC(EVP_md5(),
|
|
|
|
key.c_str(), (int) key.size(),
|
|
|
|
(unsigned char *) data.c_str(), (int) data.size(),
|
|
|
|
(unsigned char *) hash, nullptr);
|
2019-04-09 06:52:20 +02:00
|
|
|
#endif
|
2018-11-07 21:26:32 +01:00
|
|
|
|
|
|
|
std::string hashString(reinterpret_cast<char*>(hash), hashSize);
|
|
|
|
|
|
|
|
return base64_encode(hashString, (uint32_t) hashString.size());
|
|
|
|
}
|
|
|
|
}
|