Skip to content

Commit 93194f7

Browse files
replace hmac impl to use BIGNUM
1 parent a6927cb commit 93194f7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

include/jwt-cpp/jwt.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,13 +1132,14 @@ namespace jwt {
11321132
struct hmacsha {
11331133
/**
11341134
* Construct new hmac algorithm
1135-
*
1135+
*
1136+
* \deprecated Using a character is not recommeded and hardened applications should use BIGNUM
11361137
* \param key Key to use for HMAC
11371138
* \param md Pointer to hash function
11381139
* \param name Name of the algorithm
11391140
*/
11401141
hmacsha(std::string key, const EVP_MD* (*md)(), std::string name)
1141-
: secret(std::move(key)), md(md), alg_name(std::move(name)) {}
1142+
: secret(helper::raw2bn(key)), md(md), alg_name(std::move(name)) {}
11421143
/**
11431144
* Sign jwt data
11441145
*
@@ -1150,7 +1151,12 @@ namespace jwt {
11501151
ec.clear();
11511152
std::string res(static_cast<size_t>(EVP_MAX_MD_SIZE), '\0');
11521153
auto len = static_cast<unsigned int>(res.size());
1153-
if (HMAC(md(), secret.data(), static_cast<int>(secret.size()),
1154+
1155+
const int secret_size = BN_num_bytes(secret.get());
1156+
std::vector<unsigned char*> buffer(size, '\0');
1157+
BN_bn2bin(secret.get(), buffer.data());
1158+
1159+
if (HMAC(md(), secret.data(), secret_size,
11541160
reinterpret_cast<const unsigned char*>(data.data()), static_cast<int>(data.size()),
11551161
(unsigned char*)res.data(), // NOLINT(google-readability-casting) requires `const_cast`
11561162
&len) == nullptr) {
@@ -1190,7 +1196,7 @@ namespace jwt {
11901196

11911197
private:
11921198
/// HMAC secret
1193-
const std::string secret;
1199+
const std::unique_ptr<BIGNUM, decltype(&BN_free)> secret;
11941200
/// HMAC hash generator
11951201
const EVP_MD* (*md)();
11961202
/// algorithm's name

0 commit comments

Comments
 (0)