Skip to content

Commit ab42cef

Browse files
authored
refactor(structure): move sources and rename namespace
1 parent 2511896 commit ab42cef

16 files changed

Lines changed: 40 additions & 38 deletions

CMakeLists.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ option(BUILD_EXAMPLE "Build the example program" ON)
1010
option(BUILD_TESTS "Build the test suite" OFF)
1111

1212
set(HMAC_SOURCES
13-
sha1.cpp
14-
sha256.cpp
15-
sha512.cpp
16-
hmac.cpp
17-
hmac_utils.cpp
13+
src/sha1.cpp
14+
src/sha256.cpp
15+
src/sha512.cpp
16+
src/hmac.cpp
17+
src/hmac_utils.cpp
1818
)
1919

2020
set(HMAC_HEADERS
21-
hmac.hpp
22-
hmac_utils.hpp
23-
sha1.hpp
24-
sha256.hpp
25-
sha512.hpp
21+
include/hmac_cpp/hmac.hpp
22+
include/hmac_cpp/hmac_utils.hpp
23+
include/hmac_cpp/sha1.hpp
24+
include/hmac_cpp/sha256.hpp
25+
include/hmac_cpp/sha512.hpp
2626
)
2727

2828
add_library(hmac STATIC ${HMAC_SOURCES})
29-
target_include_directories(hmac PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
29+
target_include_directories(hmac PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
3030

3131
if(BUILD_EXAMPLE)
3232
add_executable(example example.cpp)
3333
target_link_libraries(example PRIVATE hmac)
34-
target_include_directories(example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
34+
target_include_directories(example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
3535
endif()
3636

3737
install(TARGETS hmac DESTINATION lib)
@@ -47,6 +47,6 @@ if(BUILD_TESTS)
4747
FetchContent_MakeAvailable(googletest)
4848
add_executable(test_all test_all.cpp)
4949
target_link_libraries(test_all PRIVATE hmac gtest_main)
50-
target_include_directories(test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
50+
target_include_directories(test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
5151
add_test(NAME test_all COMMAND test_all)
5252
endif()

README-RU.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ std::vector<uint8_t> get_hmac(
137137
#### HOTP (HMAC-based One-Time Password)
138138

139139
```cpp
140-
#include <hmac_utils.hpp>
140+
#include <hmac_cpp/hmac_utils.hpp>
141141

142142
std::string key = "12345678901234567890"; // raw key
143143
uint64_t counter = 0;
@@ -148,7 +148,7 @@ std::cout << "HOTP: " << otp << std::endl;
148148
#### TOTP (Time-based One-Time Password)
149149

150150
```
151-
#include <hmac_utils.hpp>
151+
#include <hmac_cpp/hmac_utils.hpp>
152152
153153
std::string key = "12345678901234567890"; // raw key
154154
int otp = get_totp_code(key); // по умолчанию: 30 сек, 6 цифр, SHA1
@@ -175,7 +175,7 @@ int otp = get_totp_code_at(key, time_at);
175175
Пример использования:
176176

177177
```cpp
178-
#include <hmac_utils.hpp>
178+
#include <hmac_cpp/hmac_utils.hpp>
179179

180180
std::string token = hmac::generate_time_token(secret_key, 60);
181181
bool is_valid = hmac::is_token_valid(token, secret_key, 60);
@@ -206,7 +206,7 @@ try {
206206

207207
```cpp
208208
#include <iostream>
209-
#include <hmac.hpp>
209+
#include <hmac_cpp/hmac.hpp>
210210

211211
int main() {
212212
std::string input = "grape";

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ The library supports generating one-time passwords based on RFC 4226 and RFC 623
137137
#### HOTP (HMAC-based One-Time Password)
138138

139139
```cpp
140-
#include <hmac_utils.hpp>
140+
#include <hmac_cpp/hmac_utils.hpp>
141141

142142
std::string key = "12345678901234567890"; // raw key
143143
uint64_t counter = 0;
@@ -148,7 +148,7 @@ std::cout << "HOTP: " << otp << std::endl;
148148
#### TOTP (Time-based One-Time Password)
149149

150150
```cpp
151-
#include <hmac_utils.hpp>
151+
#include <hmac_cpp/hmac_utils.hpp>
152152

153153
std::string key = "12345678901234567890"; // raw key
154154
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
175175
#### Example:
176176

177177
```cpp
178-
#include <hmac_utils.hpp>
178+
#include <hmac_cpp/hmac_utils.hpp>
179179

180180
std::string token = hmac::generate_time_token(secret_key, 60);
181181
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
206206

207207
```cpp
208208
#include <iostream>
209-
#include <hmac.hpp>
209+
#include <hmac_cpp/hmac.hpp>
210210

211211
int main() {
212212
std::string input = "grape";

example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <iostream>
22
#include <string>
33
#include <stdexcept>
4-
#include "hmac.hpp"
5-
#include "hmac_utils.hpp"
4+
#include "hmac_cpp/hmac.hpp"
5+
#include "hmac_cpp/hmac_utils.hpp"
66

77
void print_section(const std::string& title) {
88
std::cout << "=== " << title << " ===" << std::endl;

hmac.hpp renamed to include/hmac_cpp/hmac.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "sha256.hpp"
77
#include "sha512.hpp"
88

9-
namespace hmac {
9+
namespace hmac_cpp {
1010

1111
/// \brief Type of the hash function used
1212
enum class TypeHash {
@@ -80,5 +80,6 @@ namespace hmac {
8080
/// \return HMAC result
8181
std::string get_hmac(const std::string& key_input, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false);
8282
}
83+
namespace hmac = hmac_cpp;
8384

8485
#endif // _HMAC_HPP_INCLUDED
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <string>
66
#include <vector>
77

8-
namespace hmac {
8+
namespace hmac_cpp {
99

1010
/// \brief Compares two strings in constant time
1111
/// \param a First string
@@ -320,6 +320,7 @@ namespace hmac {
320320
return is_totp_token_valid(token, key.data(), key.size(), period, digits, hash_type);
321321
}
322322

323-
} // namespace hmac
323+
} // namespace hmac_cpp
324+
namespace hmac = hmac_cpp;
324325

325326
#endif // _HMAC_UTILS_HPP_INCLUDED
File renamed without changes.
File renamed without changes.
File renamed without changes.

hmac.cpp renamed to src/hmac.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <algorithm>
22
#include <stdexcept>
33
#include <cstdint>
4-
#include "hmac.hpp"
4+
#include "hmac_cpp/hmac.hpp"
55

6-
namespace hmac {
6+
namespace hmac_cpp {
77

88
std::string to_hex(const std::string& input, bool is_upper) {
99
static const char *lut = "0123456789abcdef0123456789ABCDEF";

0 commit comments

Comments
 (0)