Skip to content

Commit cf3cb42

Browse files
authored
build: hide symbols by default
2 parents c46719c + 93b7859 commit cf3cb42

7 files changed

Lines changed: 71 additions & 36 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: 7 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).
@@ -51,7 +52,7 @@ namespace hmac_cpp {
5152
}
5253

5354
/// \brief Streaming HMAC computation context.
54-
class HmacContext {
55+
class HMAC_CPP_API HmacContext {
5556
public:
5657
explicit HmacContext(TypeHash type) : type_(type), block_size_(0), digest_size_(0) {}
5758

@@ -88,7 +89,7 @@ namespace hmac_cpp {
8889
/// \param type Hash function type
8990
/// \return HMAC result as a vector of bytes
9091
/// \throws std::invalid_argument If any pointer is null while the corresponding length is non-zero
91-
std::vector<uint8_t> get_hmac(const void* key_ptr, size_t key_len, const void* msg_ptr, size_t msg_len, TypeHash type);
92+
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);
9293

9394
/// \brief Computes HMAC from key and message byte vectors using the specified hash function
9495
/// \tparam T Byte type: must be either char or uint8_t
@@ -111,7 +112,7 @@ namespace hmac_cpp {
111112
/// \param is_hex Return result in hex format
112113
/// \param is_upper Use uppercase hex
113114
/// \return HMAC result
114-
std::string get_hmac(const std::vector<uint8_t>& key, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false);
115+
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);
115116

116117
/// \brief Computes HMAC from secure_buffer key
117118
inline std::string get_hmac(const secure_buffer<uint8_t>& key, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false) {

include/hmac_cpp/hmac_utils.hpp

Lines changed: 17 additions & 17 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
inline bool constant_time_equals(const std::vector<uint8_t>& a,
@@ -77,7 +77,7 @@ namespace hmac_cpp {
7777
/// \param dk_len Desired length of the derived key in bytes, must be positive
7878
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512)
7979
/// \return Derived key as a vector of bytes
80-
std::vector<uint8_t> pbkdf2(
80+
HMAC_CPP_API std::vector<uint8_t> pbkdf2(
8181
const void* password_ptr, size_t password_len,
8282
const void* salt_ptr, size_t salt_len,
8383
uint32_t iterations, size_t dk_len,
@@ -163,7 +163,7 @@ namespace hmac_cpp {
163163
/// \param out_ptr Output buffer for derived key
164164
/// \param dk_len Length of output buffer in bytes, must be positive
165165
/// \return true on success, false on invalid parameters
166-
bool pbkdf2(Pbkdf2Hash prf,
166+
HMAC_CPP_API bool pbkdf2(Pbkdf2Hash prf,
167167
const void* password_ptr, size_t password_len,
168168
const void* salt_ptr, size_t salt_len,
169169
uint32_t iterations, uint8_t* out_ptr, size_t dk_len) noexcept;
@@ -211,7 +211,7 @@ namespace hmac_cpp {
211211
/// \param out_ptr Output buffer for derived key
212212
/// \param dk_len Length of output buffer in bytes, must be positive
213213
/// \return true on success, false on invalid parameters
214-
bool pbkdf2_hmac_sha256(const void* password_ptr, size_t password_len,
214+
HMAC_CPP_API bool pbkdf2_hmac_sha256(const void* password_ptr, size_t password_len,
215215
const void* salt_ptr, size_t salt_len,
216216
uint32_t iterations, uint8_t* out_ptr, size_t dk_len) noexcept;
217217

@@ -271,7 +271,7 @@ namespace hmac_cpp {
271271
/// \param dk_len Desired length of the derived key in bytes, must be positive.
272272
/// \param prf Hash function to use (SHA1, SHA256, SHA512).
273273
/// \return Derived key as a vector of bytes.
274-
std::vector<uint8_t> pbkdf2_with_pepper(
274+
HMAC_CPP_API std::vector<uint8_t> pbkdf2_with_pepper(
275275
const void* password_ptr, size_t password_len,
276276
const void* salt_ptr, size_t salt_len,
277277
const void* pepper_ptr, size_t pepper_len,
@@ -343,7 +343,7 @@ namespace hmac_cpp {
343343
/// \param salt_ptr Pointer to optional salt buffer (may be null when salt_len is 0).
344344
/// \param salt_len Length of the salt in bytes.
345345
/// \return Pseudorandom key (PRK) as a byte vector.
346-
std::vector<uint8_t> hkdf_extract_sha256(
346+
HMAC_CPP_API std::vector<uint8_t> hkdf_extract_sha256(
347347
const void* ikm_ptr, size_t ikm_len,
348348
const void* salt_ptr, size_t salt_len);
349349

@@ -360,7 +360,7 @@ namespace hmac_cpp {
360360
/// \param info_len Length of the info buffer in bytes.
361361
/// \param L Length of output keying material in bytes.
362362
/// \return Output keying material as a byte vector.
363-
std::vector<uint8_t> hkdf_expand_sha256(
363+
HMAC_CPP_API std::vector<uint8_t> hkdf_expand_sha256(
364364
const void* prk_ptr, size_t prk_len,
365365
const void* info_ptr, size_t info_len,
366366
size_t L);
@@ -401,7 +401,7 @@ namespace hmac_cpp {
401401
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
402402
/// \return Hex-encoded HMAC-SHA256 of the rounded time value
403403
/// \throws std::runtime_error if the system time cannot be retrieved
404-
std::string generate_time_token(const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
404+
HMAC_CPP_API std::string generate_time_token(const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
405405
inline std::string generate_time_token(const secure_buffer<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256) {
406406
return generate_time_token(std::vector<uint8_t>(key.begin(), key.end()), interval_sec, hash_type);
407407
}
@@ -418,7 +418,7 @@ namespace hmac_cpp {
418418
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
419419
/// \return true if the token is valid within the ±1 interval range; false otherwise
420420
/// \throws std::runtime_error if the system time cannot be retrieved
421-
bool is_token_valid(const std::string &token, const std::vector<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
421+
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);
422422
inline bool is_token_valid(const std::string &token, const secure_buffer<uint8_t>& key, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256) {
423423
return is_token_valid(token, std::vector<uint8_t>(key.begin(), key.end()), interval_sec, hash_type);
424424
}
@@ -435,7 +435,7 @@ namespace hmac_cpp {
435435
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
436436
/// \return Hex-encoded HMAC-SHA256 of the concatenated timestamp and fingerprint
437437
/// \throws std::runtime_error if the system time cannot be retrieved
438-
std::string generate_time_token(const std::vector<uint8_t>& key, const std::string &fingerprint, int interval_sec = 60, TypeHash hash_type = TypeHash::SHA256);
438+
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);
439439
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) {
440440
return generate_time_token(std::vector<uint8_t>(key.begin(), key.end()), fingerprint, interval_sec, hash_type);
441441
}
@@ -453,7 +453,7 @@ namespace hmac_cpp {
453453
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA256
454454
/// \return true if the token is valid within the ±1 interval range; false otherwise
455455
/// \throws std::runtime_error if the system time cannot be retrieved
456-
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);
456+
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);
457457
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) {
458458
return is_token_valid(token, std::vector<uint8_t>(key.begin(), key.end()), fingerprint, interval_sec, hash_type);
459459
}
@@ -470,7 +470,7 @@ namespace hmac_cpp {
470470
/// \param digits Desired number of digits in the OTP (typically 6–8, max 9)
471471
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512). Default is SHA1
472472
/// \return One-Time Password (OTP) as an integer in the range [0, 10^digits)
473-
int get_hotp_code(const void* key_ptr, size_t key_len, uint64_t counter, int digits = 6, TypeHash hash_type = TypeHash::SHA1);
473+
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);
474474

475475
/// \brief Computes HOTP code from a vector of bytes as key (RFC 4226)
476476
/// \tparam T Must be either `char` or `uint8_t`
@@ -508,7 +508,7 @@ namespace hmac_cpp {
508508
/// \param digits Desired number of digits in the OTP (1-9)
509509
/// \return One-Time Password (OTP) as an integer
510510
/// \throws std::runtime_error if the digest is too short for dynamic truncation
511-
int hotp_from_digest(const std::vector<uint8_t>& hmac_result, int digits);
511+
HMAC_CPP_API int hotp_from_digest(const std::vector<uint8_t>& hmac_result, int digits);
512512
}
513513

514514
/// \brief Computes TOTP (Time-Based One-Time Password) code for a specific timestamp
@@ -521,7 +521,7 @@ namespace hmac_cpp {
521521
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512; default: SHA1)
522522
/// \return TOTP code as an integer
523523
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
524-
int get_totp_code_at(
524+
HMAC_CPP_API int get_totp_code_at(
525525
const void* key_ptr,
526526
size_t key_len,
527527
uint64_t timestamp,
@@ -587,7 +587,7 @@ namespace hmac_cpp {
587587
/// \return TOTP code as an integer.
588588
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9].
589589
/// \throws std::runtime_error if the system time cannot be retrieved.
590-
int get_totp_code(
590+
HMAC_CPP_API int get_totp_code(
591591
const void* key_ptr,
592592
size_t key_len,
593593
int period = 30,
@@ -638,7 +638,7 @@ namespace hmac_cpp {
638638
/// The +1 step check is skipped when the computed counter equals
639639
/// std::numeric_limits<uint64_t>::max().
640640
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
641-
bool is_totp_token_valid(
641+
HMAC_CPP_API bool is_totp_token_valid(
642642
int token,
643643
const void* key_ptr,
644644
size_t key_len,
@@ -728,7 +728,7 @@ namespace hmac_cpp {
728728
/// std::numeric_limits<uint64_t>::max().
729729
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
730730
/// \throws std::runtime_error if the system time cannot be retrieved
731-
bool is_totp_token_valid(
731+
HMAC_CPP_API bool is_totp_token_valid(
732732
int token,
733733
const void* key_ptr,
734734
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)

include/hmac_cpp/sha512.hpp

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

5051
namespace hmac_hash {
5152

5253
/// \brief SHA512 hashing class
5354
///
5455
/// SHA-512 is the largest hash function in the SHA-2 family.
5556
/// It provides 256-bit security for digital signatures and hash-only applications.
56-
class SHA512 {
57+
class HMAC_CPP_API SHA512 {
5758
protected:
5859
const static uint64_t sha512_k[];
5960

@@ -86,18 +87,18 @@ namespace hmac_hash {
8687
/// \param data Pointer to input data
8788
/// \param length Length of the input data
8889
/// \param digest Output buffer of size SHA512::DIGEST_SIZE
89-
void sha512(const void* data, size_t length, uint8_t* digest);
90+
HMAC_CPP_API void sha512(const void* data, size_t length, uint8_t* digest);
9091

9192
/// \brief Computes SHA512 hash of a raw byte buffer
9293
/// \param data Pointer to input data
9394
/// \param length Length of the input data
9495
/// \return Vector containing the SHA512 digest
95-
std::vector<uint8_t> sha512(const void* data, size_t length);
96+
HMAC_CPP_API std::vector<uint8_t> sha512(const void* data, size_t length);
9697

9798
/// \brief Computes SHA512 hash of a string
9899
/// \param input Input string
99100
/// \return Hash as a binary string
100-
std::string sha512(const std::string &input);
101+
HMAC_CPP_API std::string sha512(const std::string &input);
101102

102103
/// \brief Computes SHA512 hash of a vector of bytes
103104
/// \tparam T Type of the vector element (char or uint8_t)

0 commit comments

Comments
 (0)