Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
8 changes: 4 additions & 4 deletions README-RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ std::vector<uint8_t> get_hmac(
#### HOTP (HMAC-based One-Time Password)

```cpp
#include <hmac_utils.hpp>
#include <hmac_cpp/hmac_utils.hpp>

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

```
#include <hmac_utils.hpp>
#include <hmac_cpp/hmac_utils.hpp>

std::string key = "12345678901234567890"; // raw key
int otp = get_totp_code(key); // по умолчанию: 30 сек, 6 цифр, SHA1
Expand All @@ -175,7 +175,7 @@ int otp = get_totp_code_at(key, time_at);
Пример использования:

```cpp
#include <hmac_utils.hpp>
#include <hmac_cpp/hmac_utils.hpp>

std::string token = hmac::generate_time_token(secret_key, 60);
bool is_valid = hmac::is_token_valid(token, secret_key, 60);
Expand Down Expand Up @@ -206,7 +206,7 @@ try {

```cpp
#include <iostream>
#include <hmac.hpp>
#include <hmac_cpp/hmac.hpp>

int main() {
std::string input = "grape";
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <hmac_utils.hpp>
#include <hmac_cpp/hmac_utils.hpp>

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

```cpp
#include <hmac_utils.hpp>
#include <hmac_cpp/hmac_utils.hpp>

std::string key = "12345678901234567890"; // raw key
int otp = get_totp_code(key); // defaults: 30s period, 6 digits, SHA1
Expand All @@ -175,7 +175,7 @@ The library also includes a **lightweight implementation of time-based HMAC toke
#### Example:

```cpp
#include <hmac_utils.hpp>
#include <hmac_cpp/hmac_utils.hpp>

std::string token = hmac::generate_time_token(secret_key, 60);
bool is_valid = hmac::is_token_valid(token, secret_key, 60);
Expand Down Expand Up @@ -206,7 +206,7 @@ The example is in `example.cpp` and is built automatically when `BUILD_EXAMPLE=O

```cpp
#include <iostream>
#include <hmac.hpp>
#include <hmac_cpp/hmac.hpp>

int main() {
std::string input = "grape";
Expand Down
4 changes: 2 additions & 2 deletions example.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include <string>
#include <stdexcept>
#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;
Expand Down
3 changes: 2 additions & 1 deletion hmac.hpp → include/hmac_cpp/hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions hmac_utils.hpp → include/hmac_cpp/hmac_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string>
#include <vector>

namespace hmac {
namespace hmac_cpp {

/// \brief Compares two strings in constant time
/// \param a First string
Expand Down Expand Up @@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions hmac.cpp → src/hmac.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <algorithm>
#include <stdexcept>
#include <cstdint>
#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";
Expand Down
8 changes: 4 additions & 4 deletions hmac_utils.cpp → src/hmac_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "hmac_utils.hpp"
#include "hmac_cpp/hmac_utils.hpp"
#include <ctime>
#include <cerrno>
#include <stdexcept>
#include <limits>

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();
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace hmac {
}

// Step 2: Compute HMAC
std::vector<uint8_t> hmac_result = hmac::get_hmac(key_ptr, key_len, counter_bytes, 8, hash_type);
std::vector<uint8_t> 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);
Expand Down Expand Up @@ -231,4 +231,4 @@ namespace hmac {
return false;
}

} // namespace hmac
} // namespace hmac_cpp
2 changes: 1 addition & 1 deletion sha1.cpp → src/sha1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <cstdio>
#include <algorithm>
#include "sha1.hpp"
#include "hmac_cpp/sha1.hpp"

/* Help macros */
#define SHA1_ROL(value, bits) (((value) << (bits)) | (((value) & 0xffffffff) >> (32 - (bits))))
Expand Down
2 changes: 1 addition & 1 deletion sha256.cpp → src/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <cstring>
#include <cstdio>
//#include <fstream>
#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)))
Expand Down
2 changes: 1 addition & 1 deletion sha512.cpp → src/sha512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <algorithm>
#include <cstring>
#include <cstdio>
#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)))
Expand Down
4 changes: 2 additions & 2 deletions test_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <limits>
#include <cerrno>

#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;
Expand Down
2 changes: 1 addition & 1 deletion test_totp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "hmac_utils.hpp"
#include "hmac_cpp/hmac_utils.hpp"
#include <cassert>
#include <limits>
#include <iostream>
Expand Down
Loading