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
20 changes: 18 additions & 2 deletions README-RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,26 @@ std::string get_hmac(
- `is_hex` — Возвращать hex-строку (`true`) или бинарные данные (`false`) [по умолчанию: true]
- `is_upper` — Использовать верхний регистр (только для `hex`) [по умолчанию: false]

Возвращает:
Если `is_hex == true`, возвращает HMAC в виде hex-строки (`std::string`).
Возвращает:
Если `is_hex == true`, возвращает HMAC в виде hex-строки (`std::string`).
Если `is_hex == false`, возвращает HMAC в виде бинарной строки (`std::string`, не предназначена для вывода).

#### Безопасная работа со строковыми ключами

Если секретный ключ получен в виде `std::string` (например, API‑ключ биржи),
переместите его в `secure_buffer`, чтобы исходная строка сразу очистилась:

```cpp
#include <cstdlib>
#include <hmac_cpp/secure_buffer.hpp>

std::string api_key = std::getenv("API_KEY");
secure_buffer key(std::move(api_key)); // api_key очищена

auto sig = hmac::get_hmac(key, payload, hmac::TypeHash::SHA256);
secure_zero(key); // при необходимости: очистить после использования
```

### HMAC (сырые бинарные данные)

```cpp
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ Returns:
If `is_hex == true`, returns a hexadecimal string (`std::string`) of the HMAC.
If `is_hex == false`, returns a raw binary HMAC as a `std::string` (not human-readable).

#### Secure handling of string keys

When a secret key is obtained as a `std::string` (e.g. an API key from an exchange),
move it into a `secure_buffer` to erase the original string immediately:

```cpp
#include <cstdlib>
#include <hmac_cpp/secure_buffer.hpp>

std::string api_key = std::getenv("API_KEY");
secure_buffer key(std::move(api_key)); // api_key is zeroized

std::vector<uint8_t> sig =
hmac::get_hmac(key, payload, hmac::TypeHash::SHA256);
secure_zero(key); // optional: wipe after use
```

### HMAC (binary data: raw buffer)

```cpp
Expand Down
18 changes: 16 additions & 2 deletions include/hmac_cpp/hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#define _HMAC_HPP_INCLUDED

#include <cstdint>
#include <string>
#include <vector>
#include "sha1.hpp"
#include "sha256.hpp"
#include "sha512.hpp"
#include "secure_buffer.hpp"

namespace hmac_cpp {

Expand Down Expand Up @@ -72,13 +75,24 @@ namespace hmac_cpp {
}

/// \brief Computes HMAC
/// \param key Secret key
/// \param key Secret key as byte vector
/// \param msg Message
/// \param type Hash function type
/// \param is_hex Return result in hex format
/// \param is_upper Use uppercase hex
/// \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);
std::string get_hmac(const std::vector<uint8_t>& key, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false);

/// \brief Computes HMAC from secure_buffer key
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) {
return get_hmac(std::vector<uint8_t>(key.begin(), key.end()), msg, type, is_hex, is_upper);
}

/// \deprecated Prefer overloads that accept std::vector<uint8_t> or secure_buffer.
HMACCPP_DEPRECATED("use std::vector<uint8_t> or secure_buffer overload")
inline std::string get_hmac(const std::string& key_input, const std::string &msg, TypeHash type, bool is_hex = true, bool is_upper = false) {
return get_hmac(std::vector<uint8_t>(key_input.begin(), key_input.end()), msg, type, is_hex, is_upper);
}
}
namespace hmac = hmac_cpp;

Expand Down
Loading
Loading