diff --git a/CMakeLists.txt b/CMakeLists.txt index b562a0f..33e5995 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,28 +10,28 @@ option(BUILD_EXAMPLE "Build the example program" ON) option(BUILD_TESTS "Build the test suite" OFF) set(HMAC_SOURCES - sha1.cpp - sha256.cpp - sha512.cpp - hmac.cpp - hmac_utils.cpp + src/sha1.cpp + src/sha256.cpp + src/sha512.cpp + src/hmac.cpp + src/hmac_utils.cpp ) set(HMAC_HEADERS - hmac.hpp - hmac_utils.hpp - sha1.hpp - sha256.hpp - sha512.hpp + include/hmac_cpp/hmac.hpp + include/hmac_cpp/hmac_utils.hpp + include/hmac_cpp/sha1.hpp + include/hmac_cpp/sha256.hpp + include/hmac_cpp/sha512.hpp ) add_library(hmac STATIC ${HMAC_SOURCES}) -target_include_directories(hmac PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(hmac PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) if(BUILD_EXAMPLE) add_executable(example example.cpp) target_link_libraries(example PRIVATE hmac) - target_include_directories(example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_include_directories(example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) endif() install(TARGETS hmac DESTINATION lib) @@ -47,6 +47,6 @@ if(BUILD_TESTS) FetchContent_MakeAvailable(googletest) add_executable(test_all test_all.cpp) target_link_libraries(test_all PRIVATE hmac gtest_main) - target_include_directories(test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_include_directories(test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) add_test(NAME test_all COMMAND test_all) endif() diff --git a/README-RU.md b/README-RU.md index fdb6ac1..46ab90e 100644 --- a/README-RU.md +++ b/README-RU.md @@ -137,7 +137,7 @@ std::vector get_hmac( #### HOTP (HMAC-based One-Time Password) ```cpp -#include +#include std::string key = "12345678901234567890"; // raw key uint64_t counter = 0; @@ -148,7 +148,7 @@ std::cout << "HOTP: " << otp << std::endl; #### TOTP (Time-based One-Time Password) ``` -#include +#include std::string key = "12345678901234567890"; // raw key int otp = get_totp_code(key); // по умолчанию: 30 сек, 6 цифр, SHA1 @@ -175,7 +175,7 @@ int otp = get_totp_code_at(key, time_at); Пример использования: ```cpp -#include +#include std::string token = hmac::generate_time_token(secret_key, 60); bool is_valid = hmac::is_token_valid(token, secret_key, 60); @@ -206,7 +206,7 @@ try { ```cpp #include -#include +#include int main() { std::string input = "grape"; diff --git a/README.md b/README.md index 0065cc9..620c3c9 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ The library supports generating one-time passwords based on RFC 4226 and RFC 623 #### HOTP (HMAC-based One-Time Password) ```cpp -#include +#include std::string key = "12345678901234567890"; // raw key uint64_t counter = 0; @@ -148,7 +148,7 @@ std::cout << "HOTP: " << otp << std::endl; #### TOTP (Time-based One-Time Password) ```cpp -#include +#include std::string key = "12345678901234567890"; // raw key int otp = get_totp_code(key); // defaults: 30s period, 6 digits, SHA1 @@ -175,7 +175,7 @@ The library also includes a **lightweight implementation of time-based HMAC toke #### Example: ```cpp -#include +#include std::string token = hmac::generate_time_token(secret_key, 60); bool is_valid = hmac::is_token_valid(token, secret_key, 60); @@ -206,7 +206,7 @@ The example is in `example.cpp` and is built automatically when `BUILD_EXAMPLE=O ```cpp #include -#include +#include int main() { std::string input = "grape"; diff --git a/example.cpp b/example.cpp index 316107d..78891e2 100644 --- a/example.cpp +++ b/example.cpp @@ -1,8 +1,8 @@ #include #include #include -#include "hmac.hpp" -#include "hmac_utils.hpp" +#include "hmac_cpp/hmac.hpp" +#include "hmac_cpp/hmac_utils.hpp" void print_section(const std::string& title) { std::cout << "=== " << title << " ===" << std::endl; diff --git a/hmac.hpp b/include/hmac_cpp/hmac.hpp similarity index 98% rename from hmac.hpp rename to include/hmac_cpp/hmac.hpp index 239d288..2f6dbf1 100644 --- a/hmac.hpp +++ b/include/hmac_cpp/hmac.hpp @@ -6,7 +6,7 @@ #include "sha256.hpp" #include "sha512.hpp" -namespace hmac { +namespace hmac_cpp { /// \brief Type of the hash function used enum class TypeHash { @@ -80,5 +80,6 @@ namespace hmac { /// \return HMAC result std::string get_hmac(const std::string& key_input, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false); } +namespace hmac = hmac_cpp; #endif // _HMAC_HPP_INCLUDED diff --git a/hmac_utils.hpp b/include/hmac_cpp/hmac_utils.hpp similarity index 99% rename from hmac_utils.hpp rename to include/hmac_cpp/hmac_utils.hpp index f99e3b8..e3ea892 100644 --- a/hmac_utils.hpp +++ b/include/hmac_cpp/hmac_utils.hpp @@ -5,7 +5,7 @@ #include #include -namespace hmac { +namespace hmac_cpp { /// \brief Compares two strings in constant time /// \param a First string @@ -320,6 +320,7 @@ namespace hmac { return is_totp_token_valid(token, key.data(), key.size(), period, digits, hash_type); } -} // namespace hmac +} // namespace hmac_cpp +namespace hmac = hmac_cpp; #endif // _HMAC_UTILS_HPP_INCLUDED diff --git a/sha1.hpp b/include/hmac_cpp/sha1.hpp similarity index 100% rename from sha1.hpp rename to include/hmac_cpp/sha1.hpp diff --git a/sha256.hpp b/include/hmac_cpp/sha256.hpp similarity index 100% rename from sha256.hpp rename to include/hmac_cpp/sha256.hpp diff --git a/sha512.hpp b/include/hmac_cpp/sha512.hpp similarity index 100% rename from sha512.hpp rename to include/hmac_cpp/sha512.hpp diff --git a/hmac.cpp b/src/hmac.cpp similarity index 99% rename from hmac.cpp rename to src/hmac.cpp index e997937..f672a0a 100644 --- a/hmac.cpp +++ b/src/hmac.cpp @@ -1,9 +1,9 @@ #include #include #include -#include "hmac.hpp" +#include "hmac_cpp/hmac.hpp" -namespace hmac { +namespace hmac_cpp { std::string to_hex(const std::string& input, bool is_upper) { static const char *lut = "0123456789abcdef0123456789ABCDEF"; diff --git a/hmac_utils.cpp b/src/hmac_utils.cpp similarity index 98% rename from hmac_utils.cpp rename to src/hmac_utils.cpp index 25ce821..b70cb99 100644 --- a/hmac_utils.cpp +++ b/src/hmac_utils.cpp @@ -1,10 +1,10 @@ -#include "hmac_utils.hpp" +#include "hmac_cpp/hmac_utils.hpp" #include #include #include #include -namespace hmac { +namespace hmac_cpp { bool constant_time_equals(const std::string &a, const std::string &b) { size_t max_len = a.size() > b.size() ? a.size() : b.size(); @@ -122,7 +122,7 @@ namespace hmac { } // Step 2: Compute HMAC - std::vector hmac_result = hmac::get_hmac(key_ptr, key_len, counter_bytes, 8, hash_type); + std::vector hmac_result = hmac_cpp::get_hmac(key_ptr, key_len, counter_bytes, 8, hash_type); // Step 3: Dynamic truncation and modulo return detail::hotp_from_digest(hmac_result, digits); @@ -231,4 +231,4 @@ namespace hmac { return false; } -} // namespace hmac +} // namespace hmac_cpp diff --git a/sha1.cpp b/src/sha1.cpp similarity index 99% rename from sha1.cpp rename to src/sha1.cpp index a529ae8..8a2f30a 100644 --- a/sha1.cpp +++ b/src/sha1.cpp @@ -21,7 +21,7 @@ #include #include -#include "sha1.hpp" +#include "hmac_cpp/sha1.hpp" /* Help macros */ #define SHA1_ROL(value, bits) (((value) << (bits)) | (((value) & 0xffffffff) >> (32 - (bits)))) diff --git a/sha256.cpp b/src/sha256.cpp similarity index 99% rename from sha256.cpp rename to src/sha256.cpp index 85e543b..93b7147 100644 --- a/sha256.cpp +++ b/src/sha256.cpp @@ -43,7 +43,7 @@ #include #include //#include -#include "sha256.hpp" +#include "hmac_cpp/sha256.hpp" #define SHA2_SHFR(x, n) (x >> n) #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) diff --git a/sha512.cpp b/src/sha512.cpp similarity index 99% rename from sha512.cpp rename to src/sha512.cpp index fab450d..2b3758b 100644 --- a/sha512.cpp +++ b/src/sha512.cpp @@ -42,7 +42,7 @@ #include #include #include -#include "sha512.hpp" +#include "hmac_cpp/sha512.hpp" #define SHA2_SHFR(x, n) (x >> n) #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) diff --git a/test_all.cpp b/test_all.cpp index d3a2c74..ca8343b 100644 --- a/test_all.cpp +++ b/test_all.cpp @@ -5,8 +5,8 @@ #include #include -#include "hmac.hpp" -#include "hmac_utils.hpp" +#include "hmac_cpp/hmac.hpp" +#include "hmac_cpp/hmac_utils.hpp" static std::time_t mock_time_value = 0; static int mock_errno_value = 0; diff --git a/test_totp.cpp b/test_totp.cpp index 1b37416..5335b82 100644 --- a/test_totp.cpp +++ b/test_totp.cpp @@ -1,4 +1,4 @@ -#include "hmac_utils.hpp" +#include "hmac_cpp/hmac_utils.hpp" #include #include #include