Skip to content

Commit 6d6ab48

Browse files
authored
Merge branch 'main' into codex/normalize-doxygen-comments-in-hmac_cpp
2 parents 9f7c316 + cf3cb42 commit 6d6ab48

7 files changed

Lines changed: 72 additions & 61 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(HMAC_SOURCES
1414
)
1515

1616
set(HMAC_HEADERS
17+
include/hmac_cpp/api.hpp
1718
include/hmac_cpp/hmac.hpp
1819
include/hmac_cpp/hmac_utils.hpp
1920
include/hmac_cpp/sha1.hpp
@@ -22,13 +23,25 @@ set(HMAC_HEADERS
2223
include/hmac_cpp/secure_buffer.hpp
2324
)
2425

25-
add_library(hmac_cpp STATIC ${HMAC_SOURCES})
26+
add_library(hmac_cpp ${HMAC_SOURCES})
2627
target_compile_features(hmac_cpp PUBLIC cxx_std_11)
2728
target_include_directories(hmac_cpp PUBLIC
2829
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
2930
$<INSTALL_INTERFACE:include>
3031
)
3132

33+
if(BUILD_SHARED_LIBS)
34+
target_compile_definitions(hmac_cpp PRIVATE HMAC_CPP_BUILD)
35+
else()
36+
target_compile_definitions(hmac_cpp PUBLIC HMAC_CPP_STATIC)
37+
endif()
38+
39+
if(MSVC)
40+
target_compile_options(hmac_cpp PRIVATE /wd4251)
41+
else()
42+
target_compile_options(hmac_cpp PRIVATE -fvisibility=hidden)
43+
endif()
44+
3245
if(NOT TARGET hmac_cpp::hmac_cpp)
3346
add_library(hmac_cpp::hmac_cpp ALIAS hmac_cpp)
3447
endif()

include/hmac_cpp/api.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef HMAC_CPP_API_HPP
2+
#define HMAC_CPP_API_HPP
3+
4+
#if defined(_WIN32) || defined(__CYGWIN__)
5+
#if defined(HMAC_CPP_STATIC)
6+
#define HMAC_CPP_API
7+
#elif defined(HMAC_CPP_BUILD)
8+
#define HMAC_CPP_API __declspec(dllexport)
9+
#else
10+
#define HMAC_CPP_API __declspec(dllimport)
11+
#endif
12+
#elif defined(__GNUC__) && __GNUC__ >= 4
13+
#define HMAC_CPP_API __attribute__((visibility("default")))
14+
#else
15+
#define HMAC_CPP_API
16+
#endif
17+
18+
#endif // HMAC_CPP_API_HPP

include/hmac_cpp/hmac.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstdint>
55
#include <string>
66
#include <vector>
7+
#include "api.hpp"
78
#include "sha1.hpp"
89
#include "sha256.hpp"
910
#include "sha512.hpp"
@@ -22,20 +23,20 @@ namespace hmac_cpp {
2223
/// \param input Input string
2324
/// \param is_upper Flag for uppercase hex
2425
/// \return Hexadecimal string
25-
std::string to_hex(const std::string& input, bool is_upper = false);
26+
HMAC_CPP_API std::string to_hex(const std::string& input, bool is_upper = false);
2627

2728
/// \brief Computes hash of the input string
2829
/// \param input Input string
2930
/// \param type Hash function type
3031
/// \return Hash result
31-
std::string get_hash(const std::string &input, TypeHash type);
32+
HMAC_CPP_API std::string get_hash(const std::string &input, TypeHash type);
3233

3334
/// \brief Computes a hash of a raw buffer using the selected hash function
3435
/// \param data Pointer to input data
3536
/// \param length Length of the input data
3637
/// \param type Hash function type
3738
/// \return Binary hash result as std::vector<uint8_t>
38-
std::vector<uint8_t> get_hash(const void* data, size_t length, TypeHash type);
39+
HMAC_CPP_API std::vector<uint8_t> get_hash(const void* data, size_t length, TypeHash type);
3940

4041
/// \brief Computes a hash of a vector using the selected hash function.
4142
/// \tparam T Type of the vector element (char or uint8_t).
@@ -52,7 +53,7 @@ namespace hmac_cpp {
5253

5354
/// \brief Streaming HMAC computation context.
5455
/// \thread_safety Not thread-safe.
55-
class HmacContext {
56+
class HMAC_CPP_API HmacContext {
5657
public:
5758
/// \brief Construct context for selected hash.
5859
/// \param type Hash function type.
@@ -91,7 +92,7 @@ namespace hmac_cpp {
9192
/// \param type Hash function type
9293
/// \return HMAC result as a vector of bytes
9394
/// \throws std::invalid_argument If any pointer is null while the corresponding length is non-zero
94-
std::vector<uint8_t> get_hmac(const void* key_ptr, size_t key_len, const void* msg_ptr, size_t msg_len, TypeHash type);
95+
HMAC_CPP_API std::vector<uint8_t> get_hmac(const void* key_ptr, size_t key_len, const void* msg_ptr, size_t msg_len, TypeHash type);
9596

9697
/// \brief Computes HMAC from key and message byte vectors using the specified hash function
9798
/// \tparam T Byte type: must be either char or uint8_t
@@ -114,7 +115,7 @@ namespace hmac_cpp {
114115
/// \param is_hex Return result in hex format
115116
/// \param is_upper Use uppercase hex
116117
/// \return HMAC result
117-
std::string get_hmac(const std::vector<uint8_t>& key, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false);
118+
HMAC_CPP_API std::string get_hmac(const std::vector<uint8_t>& key, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false);
118119

119120
/// \brief Compute HMAC using secure_buffer key.
120121
/// \param key Secret key bytes.
@@ -140,6 +141,7 @@ namespace hmac_cpp {
140141
return get_hmac(std::vector<uint8_t>(key_input.begin(), key_input.end()), msg, type, is_hex, is_upper);
141142
}
142143
}
144+
143145
namespace hmac = hmac_cpp;
144146

145147
#endif // _HMAC_HPP_INCLUDED

include/hmac_cpp/hmac_utils.hpp

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace hmac_cpp {
2121
/// \param b Pointer to second array
2222
/// \param b_len Length of the second array
2323
/// \return true if both arrays are equal
24-
bool constant_time_equals(const uint8_t* a, size_t a_len,
24+
HMAC_CPP_API bool constant_time_equals(const uint8_t* a, size_t a_len,
2525
const uint8_t* b, size_t b_len);
2626

2727
/// \brief Compare vectors in constant time.
@@ -98,7 +98,7 @@ namespace hmac_cpp {
9898
/// \param dk_len Desired length of the derived key in bytes, must be positive
9999
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512)
100100
/// \return Derived key as a vector of bytes
101-
std::vector<uint8_t> pbkdf2(
101+
HMAC_CPP_API std::vector<uint8_t> pbkdf2(
102102
const void* password_ptr, size_t password_len,
103103
const void* salt_ptr, size_t salt_len,
104104
uint32_t iterations, size_t dk_len,
@@ -220,7 +220,7 @@ namespace hmac_cpp {
220220
/// \param out_ptr Output buffer for derived key
221221
/// \param dk_len Length of output buffer in bytes, must be positive
222222
/// \return true on success, false on invalid parameters
223-
bool pbkdf2(Pbkdf2Hash prf,
223+
HMAC_CPP_API bool pbkdf2(Pbkdf2Hash prf,
224224
const void* password_ptr, size_t password_len,
225225
const void* salt_ptr, size_t salt_len,
226226
uint32_t iterations, uint8_t* out_ptr, size_t dk_len) noexcept;
@@ -292,7 +292,7 @@ namespace hmac_cpp {
292292
/// \param out_ptr Output buffer for derived key
293293
/// \param dk_len Length of output buffer in bytes, must be positive
294294
/// \return true on success, false on invalid parameters
295-
bool pbkdf2_hmac_sha256(const void* password_ptr, size_t password_len,
295+
HMAC_CPP_API bool pbkdf2_hmac_sha256(const void* password_ptr, size_t password_len,
296296
const void* salt_ptr, size_t salt_len,
297297
uint32_t iterations, uint8_t* out_ptr, size_t dk_len) noexcept;
298298

@@ -359,7 +359,7 @@ namespace hmac_cpp {
359359
/// \param dk_len Desired length of the derived key in bytes, must be positive.
360360
/// \param prf Hash function to use (SHA1, SHA256, SHA512).
361361
/// \return Derived key as a vector of bytes.
362-
std::vector<uint8_t> pbkdf2_with_pepper(
362+
HMAC_CPP_API std::vector<uint8_t> pbkdf2_with_pepper(
363363
const void* password_ptr, size_t password_len,
364364
const void* salt_ptr, size_t salt_len,
365365
const void* pepper_ptr, size_t pepper_len,
@@ -438,7 +438,7 @@ namespace hmac_cpp {
438438
/// \param salt_ptr Pointer to optional salt buffer (may be null when salt_len is 0).
439439
/// \param salt_len Length of the salt in bytes.
440440
/// \return Pseudorandom key (PRK) as a byte vector.
441-
std::vector<uint8_t> hkdf_extract_sha256(
441+
HMAC_CPP_API std::vector<uint8_t> hkdf_extract_sha256(
442442
const void* ikm_ptr, size_t ikm_len,
443443
const void* salt_ptr, size_t salt_len);
444444

@@ -459,7 +459,7 @@ namespace hmac_cpp {
459459
/// \param info_len Length of the info buffer in bytes.
460460
/// \param L Length of output keying material in bytes.
461461
/// \return Output keying material as a byte vector.
462-
std::vector<uint8_t> hkdf_expand_sha256(
462+
HMAC_CPP_API std::vector<uint8_t> hkdf_expand_sha256(
463463
const void* prk_ptr, size_t prk_len,
464464
const void* info_ptr, size_t info_len,
465465
size_t L);
@@ -510,13 +510,8 @@ namespace hmac_cpp {
510510
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
511511
/// \return Hex-encoded HMAC-SHA256 of the rounded time value
512512
/// \throws std::runtime_error if the system time cannot be retrieved
513-
std::string generate_time_token(const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
513+
HMAC_CPP_API std::string generate_time_token(const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
514514

515-
/// \brief Generate time token using secure buffer key.
516-
/// \param key Secret key bytes.
517-
/// \param interval_sec Token rotation interval in seconds.
518-
/// \param hash_type Hash function to use.
519-
/// \return Hex-encoded token.
520515
inline std::string generate_time_token(const secure_buffer<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256) {
521516
return generate_time_token(std::vector<uint8_t>(key.begin(), key.end()), interval_sec, hash_type);
522517
}
@@ -539,14 +534,7 @@ namespace hmac_cpp {
539534
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
540535
/// \return true if the token is valid within the ±1 interval range; false otherwise
541536
/// \throws std::runtime_error if the system time cannot be retrieved
542-
bool is_token_valid(const std::string &token, const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
543-
544-
/// \brief Validate time token using secure buffer key.
545-
/// \param token Token to validate.
546-
/// \param key Secret key bytes.
547-
/// \param interval_sec Token rotation interval in seconds.
548-
/// \param hash_type Hash function to use.
549-
/// \return true if token is valid.
537+
HMAC_CPP_API bool is_token_valid(const std::string &token, const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
550538
inline bool is_token_valid(const std::string &token, const secure_buffer<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256) {
551539
return is_token_valid(token, std::vector<uint8_t>(key.begin(), key.end()), interval_sec, hash_type);
552540
}
@@ -570,14 +558,8 @@ namespace hmac_cpp {
570558
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
571559
/// \return Hex-encoded HMAC-SHA256 of the concatenated timestamp and fingerprint
572560
/// \throws std::runtime_error if the system time cannot be retrieved
573-
std::string generate_time_token(const std::vector<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
561+
HMAC_CPP_API std::string generate_time_token(const std::vector<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
574562

575-
/// \brief Generate fingerprint-bound token using secure buffer key.
576-
/// \param key Secret key bytes.
577-
/// \param fingerprint Client identifier.
578-
/// \param interval_sec Token rotation interval in seconds.
579-
/// \param hash_type Hash function to use.
580-
/// \return Hex-encoded token.
581563
inline std::string generate_time_token(const secure_buffer<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256) {
582564
return generate_time_token(std::vector<uint8_t>(key.begin(), key.end()), fingerprint, interval_sec, hash_type);
583565
}
@@ -602,15 +584,8 @@ namespace hmac_cpp {
602584
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
603585
/// \return true if the token is valid within the ±1 interval range; false otherwise
604586
/// \throws std::runtime_error if the system time cannot be retrieved
605-
bool is_token_valid(const std::string &token, const std::vector<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
587+
HMAC_CPP_API bool is_token_valid(const std::string &token, const std::vector<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
606588

607-
/// \brief Validate fingerprint-bound token using secure buffer key.
608-
/// \param token Token to validate.
609-
/// \param key Secret key bytes.
610-
/// \param fingerprint Client identifier.
611-
/// \param interval_sec Token rotation interval in seconds.
612-
/// \param hash_type Hash function to use.
613-
/// \return true if token is valid.
614589
inline bool is_token_valid(const std::string &token, const secure_buffer<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256) {
615590
return is_token_valid(token, std::vector<uint8_t>(key.begin(), key.end()), fingerprint, interval_sec, hash_type);
616591
}
@@ -635,7 +610,7 @@ namespace hmac_cpp {
635610
/// \param digits Desired number of digits in the OTP (typically 6–8, max 9)
636611
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA1
637612
/// \return One-Time Password (OTP) as an integer in the range [0, 10^digits)
638-
int get_hotp_code(const void* key_ptr, size_t key_len, uint64_t counter, int digits = 6, TypeHash hash_type = TypeHash::SHA1);
613+
HMAC_CPP_API int get_hotp_code(const void* key_ptr, size_t key_len, uint64_t counter, int digits = 6, TypeHash hash_type = TypeHash::SHA1);
639614

640615
/// \brief Computes HOTP code from a vector of bytes as key (RFC 4226)
641616
/// \tparam T Must be either `char` or `uint8_t`
@@ -679,7 +654,7 @@ namespace hmac_cpp {
679654
/// \param digits Desired number of digits in the OTP (1-9)
680655
/// \return One-Time Password (OTP) as an integer
681656
/// \throws std::runtime_error if the digest is too short for dynamic truncation
682-
int hotp_from_digest(const std::vector<uint8_t>& hmac_result, int digits);
657+
HMAC_CPP_API int hotp_from_digest(const std::vector<uint8_t>& hmac_result, int digits);
683658
}
684659

685660
/// \brief Computes TOTP (Time-Based One-Time Password) code for a specific timestamp
@@ -692,7 +667,7 @@ namespace hmac_cpp {
692667
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512; default: SHA1)
693668
/// \return TOTP code as an integer
694669
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
695-
int get_totp_code_at(
670+
HMAC_CPP_API int get_totp_code_at(
696671
const void* key_ptr,
697672
size_t key_len,
698673
uint64_t timestamp,
@@ -765,7 +740,7 @@ namespace hmac_cpp {
765740
/// \return TOTP code as an integer.
766741
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9].
767742
/// \throws std::runtime_error if the system time cannot be retrieved.
768-
int get_totp_code(
743+
HMAC_CPP_API int get_totp_code(
769744
const void* key_ptr,
770745
size_t key_len,
771746
int period = 30,
@@ -822,7 +797,7 @@ namespace hmac_cpp {
822797
/// The +1 step check is skipped when the computed counter equals
823798
/// std::numeric_limits<uint64_t>::max().
824799
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
825-
bool is_totp_token_valid(
800+
HMAC_CPP_API bool is_totp_token_valid(
826801
int token,
827802
const void* key_ptr,
828803
size_t key_len,
@@ -912,7 +887,7 @@ namespace hmac_cpp {
912887
/// std::numeric_limits<uint64_t>::max().
913888
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
914889
/// \throws std::runtime_error if the system time cannot be retrieved
915-
bool is_totp_token_valid(
890+
HMAC_CPP_API bool is_totp_token_valid(
916891
int token,
917892
const void* key_ptr,
918893
size_t key_len,

include/hmac_cpp/sha1.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
#include <string>
2525
#include <vector>
2626
#include <cstdint>
27+
#include "api.hpp"
2728

2829
namespace hmac_hash {
2930

3031
/// \brief Class for computing SHA1 hash
31-
class SHA1 {
32+
class HMAC_CPP_API SHA1 {
3233
public:
3334

3435
/// \brief Initializes SHA1 context
@@ -60,18 +61,18 @@ namespace hmac_hash {
6061
/// \param data Pointer to input data
6162
/// \param length Length of the input data
6263
/// \param digest Output buffer of size SHA1::DIGEST_SIZE
63-
void sha1(const void* data, size_t length, uint8_t* digest);
64+
HMAC_CPP_API void sha1(const void* data, size_t length, uint8_t* digest);
6465

6566
/// \brief Computes SHA1 hash of a raw byte buffer
6667
/// \param data Pointer to input data
6768
/// \param length Length of the input data
6869
/// \return Vector containing the SHA1 digest
69-
std::vector<uint8_t> sha1(const void* data, size_t length);
70+
HMAC_CPP_API std::vector<uint8_t> sha1(const void* data, size_t length);
7071

7172
/// \brief Computes SHA1 hash of a string
7273
/// \param input Input string
7374
/// \return Hash as a binary string
74-
std::string sha1(const std::string &input);
75+
HMAC_CPP_API std::string sha1(const std::string &input);
7576

7677
/// \brief Computes SHA1 hash of a vector of bytes
7778
/// \tparam T Type of the vector element (char or uint8_t)

include/hmac_cpp/sha256.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@
4646
#include <string>
4747
#include <vector>
4848
#include <cstdint>
49+
#include "api.hpp"
4950

5051
namespace hmac_hash {
5152

5253
/// \brief Class for computing SHA256 hash
53-
class SHA256 {
54+
class HMAC_CPP_API SHA256 {
5455
protected:
5556
const static uint32_t sha256_k[];
5657

@@ -83,18 +84,18 @@ namespace hmac_hash {
8384
/// \param data Pointer to input data
8485
/// \param length Length of the input data
8586
/// \param digest Output buffer of size SHA256::DIGEST_SIZE
86-
void sha256(const void* data, size_t length, uint8_t* digest);
87+
HMAC_CPP_API void sha256(const void* data, size_t length, uint8_t* digest);
8788

8889
/// \brief Computes SHA256 hash of a raw byte buffer
8990
/// \param data Pointer to input data
9091
/// \param length Length of the input data
9192
/// \return Vector containing the SHA256 digest
92-
std::vector<uint8_t> sha256(const void* data, size_t length);
93+
HMAC_CPP_API std::vector<uint8_t> sha256(const void* data, size_t length);
9394

9495
/// \brief Computes SHA256 hash of a string
9596
/// \param input Input string
9697
/// \return Hash as a binary string
97-
std::string sha256(const std::string &input);
98+
HMAC_CPP_API std::string sha256(const std::string &input);
9899

99100
/// \brief Computes SHA256 hash of a vector of bytes
100101
/// \tparam T Type of the vector element (char or uint8_t)

0 commit comments

Comments
 (0)