Skip to content

Commit 3c82e46

Browse files
hmacsha needs to be copyable at ctor
1 parent 1bc4f0b commit 3c82e46

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

include/jwt-cpp/jwt.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,18 @@ namespace jwt {
11441144
*/
11451145
hmacsha(std::string key, const EVP_MD* (*md)(), std::string name)
11461146
: secret(helper::raw2bn(key)), md(md), alg_name(std::move(name)) {}
1147+
hmacsha(const hmacsha& other) :
1148+
secret(BN_dup(other.secret)), md(other.md), alg_name(other.alg_name) {
1149+
}
1150+
hmacsha(hmacsha&& other) :
1151+
secret(BN_copy(other.secret)), md(std::move(other.md)), alg_name(std::move(other.alg_name)) {
1152+
}
1153+
~hmacsha(){
1154+
BN_free(secret)
1155+
}
1156+
hmacsha& operator=(const hmacsha& other) = default;
1157+
hmacsha& operator=(hmacsha&& other) = default;
1158+
11471159
/**
11481160
* Sign jwt data
11491161
*
@@ -1156,9 +1168,8 @@ namespace jwt {
11561168
std::string res(static_cast<size_t>(EVP_MAX_MD_SIZE), '\0');
11571169
auto len = static_cast<unsigned int>(res.size());
11581170

1159-
const BIGNUM* secret_bn = secret.get();
1160-
std::vector<unsigned char> buffer(BN_num_bytes(secret_bn), '\0');
1161-
const auto buffer_size = BN_bn2bin(secret_bn, buffer.data());
1171+
std::vector<unsigned char> buffer(BN_num_bytes(secret), '\0');
1172+
const auto buffer_size = BN_bn2bin(secret, buffer.data());
11621173
buffer.resize(buffer_size);
11631174

11641175
if (HMAC(md(), buffer.data(), buffer_size,
@@ -1201,7 +1212,7 @@ namespace jwt {
12011212

12021213
private:
12031214
/// HMAC secret
1204-
const std::unique_ptr<BIGNUM, decltype(&BN_free)> secret;
1215+
const BIGNUM* secret;
12051216
/// HMAC hash generator
12061217
const EVP_MD* (*md)();
12071218
/// algorithm's name

0 commit comments

Comments
 (0)