Skip to content

Commit a8bf6cc

Browse files
committed
refactor(mql): rename hash enum values
Aligns MQL TypeHash names with C++ API by renaming HASH_SHA256 and HASH_SHA512 to SHA256 and SHA512. Updates MQL tests and utilities, and refreshes documentation with a mapping table.
1 parent 4b51dfb commit a8bf6cc

5 files changed

Lines changed: 29 additions & 17 deletions

File tree

README-RU.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ _install/
6767
```mql5
6868
#include <hmac-cpp/hmac.mqh>
6969
70-
string hash = hmac::get_hmac("key", "message", hmac::TypeHash::HASH_SHA256);
70+
string hash = hmac::get_hmac("key", "message", hmac::TypeHash::SHA256);
7171
```
7272

73+
| Хеш-функция | Значение в C++ | Значение в MQL |
74+
|-------------|--------------------------|--------------------------|
75+
| SHA1 | `hmac::TypeHash::SHA1` | – (не доступно) |
76+
| SHA256 | `hmac::TypeHash::SHA256` | `hmac::TypeHash::SHA256` |
77+
| SHA512 | `hmac::TypeHash::SHA512` | `hmac::TypeHash::SHA512` |
78+
7379
## Использование
7480

7581
### HMAC (ввод в виде строки)

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ You can use the same interface inside your MQL5 scripts and experts:
9595
```mql5
9696
#include <hmac-cpp/hmac.mqh>
9797
98-
string hash = hmac::get_hmac("key", "message", hmac::TypeHash::HASH_SHA256);
98+
string hash = hmac::get_hmac("key", "message", hmac::TypeHash::SHA256);
9999
```
100100

101+
| Hash function | C++ enum | MQL enum |
102+
|---------------|----------------------|-----------------------|
103+
| SHA1 | `hmac::TypeHash::SHA1`| – (not available) |
104+
| SHA256 | `hmac::TypeHash::SHA256` | `hmac::TypeHash::SHA256` |
105+
| SHA512 | `hmac::TypeHash::SHA512` | `hmac::TypeHash::SHA512` |
106+
101107
## Usage
102108

103109
### HMAC (string input)

hmac.mqh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace hmac {
1313

1414
/// \brief Type of the hash function used
1515
enum TypeHash {
16-
HASH_SHA256 = 0, ///< Use SHA256
17-
HASH_SHA512 = 1, ///< Use SHA512
16+
SHA256 = 0, ///< Use SHA256
17+
SHA512 = 1, ///< Use SHA512
1818
};
1919

2020
/// \brief Converts a byte array to hexadecimal string
@@ -66,7 +66,7 @@ namespace hmac {
6666
/// \param type Hash function type
6767
void calc_hash(uchar &digest[], const uchar &data[], TypeHash type) {
6868
switch(type) {
69-
case TypeHash::HASH_SHA256: {
69+
case TypeHash::SHA256: {
7070
ArrayResize(digest, SHA256_DIGEST_SIZE);
7171
ArrayFill(digest, 0, SHA256_DIGEST_SIZE, 0);
7272
hmac_hash::SHA256 ctx;
@@ -75,7 +75,7 @@ namespace hmac {
7575
ctx.finish(digest);
7676
break;
7777
}
78-
case TypeHash::HASH_SHA512: {
78+
case TypeHash::SHA512: {
7979
ArrayResize(digest, SHA512_DIGEST_SIZE);
8080
ArrayFill(digest, 0, SHA512_DIGEST_SIZE, 0);
8181
hmac_hash::SHA512 ctx;
@@ -116,11 +116,11 @@ namespace hmac {
116116
int block_size = 0;
117117
int digest_size = 0;
118118
switch(type) {
119-
case TypeHash::HASH_SHA256:
119+
case TypeHash::SHA256:
120120
block_size = SHA224_256_BLOCK_SIZE;
121121
digest_size = SHA256_DIGEST_SIZE;
122122
break;
123-
case TypeHash::HASH_SHA512:
123+
case TypeHash::SHA512:
124124
block_size = SHA384_512_BLOCK_SIZE;
125125
digest_size = SHA512_DIGEST_SIZE;
126126
break;

hmac_utils.mqh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace hmac {
1515
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
1616
/// \param hash_type Hash function to use. Default is SHA256
1717
/// \return Hex-encoded HMAC-SHA256 of the rounded time value
18-
string generate_time_token(const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
18+
string generate_time_token(const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
1919
datetime now = TimeGMT();
2020
datetime rounded = (now / interval_sec) * interval_sec;
2121
return hmac::get_hmac(key, IntegerToString(rounded), hash_type);
@@ -27,7 +27,7 @@ namespace hmac {
2727
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
2828
/// \param hash_type Hash function to use. Default is SHA256
2929
/// \return true if the token is valid within the ±1 interval range; false otherwise
30-
bool is_token_valid(const string &token, const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
30+
bool is_token_valid(const string &token, const string &key, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
3131
datetime now = TimeGMT();
3232
datetime rounded = (now / interval_sec) * interval_sec;
3333
if (token == get_hmac(key, IntegerToString(rounded), hash_type)) return true;
@@ -42,7 +42,7 @@ namespace hmac {
4242
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
4343
/// \param hash_type Hash function to use. Default is SHA256
4444
/// \return Hex-encoded HMAC-SHA256 of the concatenated timestamp and fingerprint
45-
string generate_time_token(const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::HASH_SHA256) {
45+
string generate_time_token(const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
4646
datetime now = TimeGMT();
4747
datetime rounded = (now / interval_sec) * interval_sec;
4848
string payload = IntegerToString(rounded) + "|" + fingerprint;
@@ -56,7 +56,7 @@ namespace hmac {
5656
/// \param interval_sec Interval in seconds that defines token rotation. Default is 60 seconds
5757
/// \param hash_type Hash function to use. Default is SHA256
5858
/// \return true if the token is valid within the ±1 interval range; false otherwise
59-
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) {
59+
bool is_token_valid(const string &token, const string &key, const string &fingerprint, int interval_sec = 60, hmac::TypeHash hash_type = hmac::TypeHash::SHA256) {
6060
datetime now = TimeGMT();
6161
datetime rounded = (now / interval_sec) * interval_sec;
6262
string prefix = "|" + fingerprint;

test_hmac.mq5

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ void OnStart() {
1111

1212
// SHA256
1313
print_section("SHA256");
14-
string sha256_output_1 = hmac::get_hash(data, hmac::TypeHash::HASH_SHA256);
14+
string sha256_output_1 = hmac::get_hash(data, hmac::TypeHash::SHA256);
1515
string sha256_output_2 = hmac_hash::sha256(data);
1616
Print("sha256('", data, "') = ", sha256_output_1);
1717
Print("sha256('", data, "') = ", sha256_output_2);
1818
Print("Expected: 0f78fcc486f5315418fbf095e71c0675ee07d318e5ac4d150050cd8e57966496\n");
1919

2020
// SHA512
2121
print_section("SHA512");
22-
string sha512_output_1 = hmac::get_hash(data, hmac::TypeHash::HASH_SHA512);
22+
string sha512_output_1 = hmac::get_hash(data, hmac::TypeHash::SHA512);
2323
string sha512_output_2 = hmac_hash::sha512(data);
2424
Print("sha512('", data, "') = ", sha512_output_1);
2525
Print("sha512('", data, "') = ", sha512_output_2);
@@ -32,19 +32,19 @@ void OnStart() {
3232

3333
// HMAC-SHA256
3434
print_section("HMAC-SHA256");
35-
string hmac_sha256 = hmac::get_hmac(key, data, hmac::TypeHash::HASH_SHA256);
35+
string hmac_sha256 = hmac::get_hmac(key, data, hmac::TypeHash::SHA256);
3636
Print("HMAC('", key, "', '", data, "', SHA256) = ", hmac_sha256);
3737
Print("Expected: 7632ac2e8ddedaf4b3e7ab195fefd17571c37c970e02e169195a158ef59e53ca\n");
3838

3939
// HMAC-SHA512
4040
print_section("HMAC-SHA512");
41-
string hmac_sha512 = hmac::get_hmac(key, data, hmac::TypeHash::HASH_SHA512);
41+
string hmac_sha512 = hmac::get_hmac(key, data, hmac::TypeHash::SHA512);
4242
Print("HMAC('", key, "', '", data, "', SHA512) = ", hmac_sha512);
4343
Print("Expected: c54ddf9647a949d0df925a1c1f8ba1c9d721a671c396fde1062a71f9f7ffae5dc10f6be15be63bb0363d051365e23f890368c54828497b9aef2eb2fc65b633e6\n");
4444

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

5050
// HMAC-TIMED TOKEN

0 commit comments

Comments
 (0)