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
8 changes: 7 additions & 1 deletion README-RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ _install/
```mql5
#include <hmac-cpp/hmac.mqh>

string hash = hmac::get_hmac("key", "message", hmac::TypeHash::HASH_SHA256);
string hash = hmac::get_hmac("key", "message", hmac::TypeHash::SHA256);
```

| Хеш-функция | Значение в C++ | Значение в MQL |
|-------------|--------------------------|--------------------------|
| SHA1 | `hmac::TypeHash::SHA1` | – (не доступно) |
| SHA256 | `hmac::TypeHash::SHA256` | `hmac::TypeHash::SHA256` |
| SHA512 | `hmac::TypeHash::SHA512` | `hmac::TypeHash::SHA512` |

## Использование

### HMAC (ввод в виде строки)
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ You can use the same interface inside your MQL5 scripts and experts:
```mql5
#include <hmac-cpp/hmac.mqh>

string hash = hmac::get_hmac("key", "message", hmac::TypeHash::HASH_SHA256);
string hash = hmac::get_hmac("key", "message", hmac::TypeHash::SHA256);
```

| Hash function | C++ enum | MQL enum |
|---------------|----------------------|-----------------------|
| SHA1 | `hmac::TypeHash::SHA1`| – (not available) |
| SHA256 | `hmac::TypeHash::SHA256` | `hmac::TypeHash::SHA256` |
| SHA512 | `hmac::TypeHash::SHA512` | `hmac::TypeHash::SHA512` |

## Usage

### HMAC (string input)
Expand Down
12 changes: 6 additions & 6 deletions hmac.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace hmac {

/// \brief Type of the hash function used
enum TypeHash {
HASH_SHA256 = 0, ///< Use SHA256
HASH_SHA512 = 1, ///< Use SHA512
SHA256 = 0, ///< Use SHA256
SHA512 = 1, ///< Use SHA512
};

/// \brief Converts a byte array to hexadecimal string
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace hmac {
/// \param type Hash function type
void calc_hash(uchar &digest[], const uchar &data[], TypeHash type) {
switch(type) {
case TypeHash::HASH_SHA256: {
case TypeHash::SHA256: {
ArrayResize(digest, SHA256_DIGEST_SIZE);
ArrayFill(digest, 0, SHA256_DIGEST_SIZE, 0);
hmac_hash::SHA256 ctx;
Expand All @@ -75,7 +75,7 @@ namespace hmac {
ctx.finish(digest);
break;
}
case TypeHash::HASH_SHA512: {
case TypeHash::SHA512: {
ArrayResize(digest, SHA512_DIGEST_SIZE);
ArrayFill(digest, 0, SHA512_DIGEST_SIZE, 0);
hmac_hash::SHA512 ctx;
Expand Down Expand Up @@ -116,11 +116,11 @@ namespace hmac {
int block_size = 0;
int digest_size = 0;
switch(type) {
case TypeHash::HASH_SHA256:
case TypeHash::SHA256:
block_size = SHA224_256_BLOCK_SIZE;
digest_size = SHA256_DIGEST_SIZE;
break;
case TypeHash::HASH_SHA512:
case TypeHash::SHA512:
block_size = SHA384_512_BLOCK_SIZE;
digest_size = SHA512_DIGEST_SIZE;
break;
Expand Down
8 changes: 4 additions & 4 deletions hmac_utils.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace hmac {
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
/// \param hash_type Hash function to use. Default is SHA256
/// \return Hex-encoded HMAC-SHA256 of the rounded time value
string generate_time_token(const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
string generate_time_token(const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
datetime now = TimeGMT();
datetime rounded = (now / interval_sec) * interval_sec;
return hmac::get_hmac(key, IntegerToString(rounded), hash_type);
Expand All @@ -27,7 +27,7 @@ namespace hmac {
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
/// \param hash_type Hash function to use. Default is SHA256
/// \return true if the token is valid within the ±1 interval range; false otherwise
bool is_token_valid(const string &token, const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
bool is_token_valid(const string &token, const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
datetime now = TimeGMT();
datetime rounded = (now / interval_sec) * interval_sec;
if (token == get_hmac(key, IntegerToString(rounded), hash_type)) return true;
Expand All @@ -42,7 +42,7 @@ namespace hmac {
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
/// \param hash_type Hash function to use. Default is SHA256
/// \return Hex-encoded HMAC-SHA256 of the concatenated timestamp and fingerprint
string generate_time_token(const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
string generate_time_token(const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
datetime now = TimeGMT();
datetime rounded = (now / interval_sec) * interval_sec;
string payload = IntegerToString(rounded) + "|" + fingerprint;
Expand All @@ -56,7 +56,7 @@ namespace hmac {
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
/// \param hash_type Hash function to use. Default is SHA256
/// \return true if the token is valid within the ±1 interval range; false otherwise
bool is_token_valid(const string &token, const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
bool is_token_valid(const string &token, const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
datetime now = TimeGMT();
datetime rounded = (now / interval_sec) * interval_sec;
string prefix = "|" + fingerprint;
Expand Down
10 changes: 5 additions & 5 deletions test_hmac.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ void OnStart() {

// SHA256
print_section("SHA256");
string sha256_output_1 = hmac::get_hash(data, hmac::TypeHash::HASH_SHA256);
string sha256_output_1 = hmac::get_hash(data, hmac::TypeHash::SHA256);
string sha256_output_2 = hmac_hash::sha256(data);
Print("sha256('", data, "') = ", sha256_output_1);
Print("sha256('", data, "') = ", sha256_output_2);
Print("Expected: 0f78fcc486f5315418fbf095e71c0675ee07d318e5ac4d150050cd8e57966496\n");

// SHA512
print_section("SHA512");
string sha512_output_1 = hmac::get_hash(data, hmac::TypeHash::HASH_SHA512);
string sha512_output_1 = hmac::get_hash(data, hmac::TypeHash::SHA512);
string sha512_output_2 = hmac_hash::sha512(data);
Print("sha512('", data, "') = ", sha512_output_1);
Print("sha512('", data, "') = ", sha512_output_2);
Expand All @@ -32,19 +32,19 @@ void OnStart() {

// HMAC-SHA256
print_section("HMAC-SHA256");
string hmac_sha256 = hmac::get_hmac(key, data, hmac::TypeHash::HASH_SHA256);
string hmac_sha256 = hmac::get_hmac(key, data, hmac::TypeHash::SHA256);
Print("HMAC('", key, "', '", data, "', SHA256) = ", hmac_sha256);
Print("Expected: 7632ac2e8ddedaf4b3e7ab195fefd17571c37c970e02e169195a158ef59e53ca\n");

// HMAC-SHA512
print_section("HMAC-SHA512");
string hmac_sha512 = hmac::get_hmac(key, data, hmac::TypeHash::HASH_SHA512);
string hmac_sha512 = hmac::get_hmac(key, data, hmac::TypeHash::SHA512);
Print("HMAC('", key, "', '", data, "', SHA512) = ", hmac_sha512);
Print("Expected: c54ddf9647a949d0df925a1c1f8ba1c9d721a671c396fde1062a71f9f7ffae5dc10f6be15be63bb0363d051365e23f890368c54828497b9aef2eb2fc65b633e6\n");

// HMAC-SHA512 uppercase hex
print_section("HMAC-SHA512 (uppercase)");
string hmac_sha512_upper = hmac::get_hmac(key, data, hmac::TypeHash::HASH_SHA512, true);
string hmac_sha512_upper = hmac::get_hmac(key, data, hmac::TypeHash::SHA512, true);
Print("HMAC('", key, "', '", data, "', SHA512, hex=true, upper=true) = ", hmac_sha512_upper);

// HMAC-TIMED TOKEN
Expand Down
Loading