AesHmac encrypts texts and bytes in MQL4 so that only a program holding the same key can read them. It is one include file with no other dependencies, built on the terminal's own CryptEncode. Use it for an API token in a settings file, a licence string, or a message two of your programs pass through a file or a web request.
Encryption alone does not tell you whether someone changed the message. AesHmac adds a tag that does: a wrong key, one changed character or text that was never sealed is refused with an error code, never decrypted into garbage that your program then tries to use.
#include <AesHmac\AesHmac.mqh>
#include <AesHmac\AesHmac.mqh> void OnStart() { uchar key[]; if (AesHmacKeyFromPassword("a long password", "MyEA", AES_HMAC_DEFAULT_ITERATIONS, key) != AES_HMAC_OK) { return; } string sealed; AesHmacSeal(key, "{\"api_token\":\"tk_5f2c9a71\"}", sealed); Print(sealed); // base64, different every time string text; int err = AesHmacOpen(key, sealed, text); if (err != AES_HMAC_OK) { Print("refused: ", AesHmacErrorText(err)); return; } Print(text); // {"api_token":"tk_5f2c9a71"} }
The attached AesHmacExample script runs the self test, makes a key from a password, seals a secret twice, saves it to MQL4\Files\AesHmac\secret.txt, reads it back and opens it, then shows the three refusals:
self test aes_block=ok ctr=ok hmac=ok pbkdf2=ok
key made iterations=10000 ms=27
sealed again same_as_first=false
saved and opened file=AesHmac\secret.txt text={"api_token":"tk_5f2c9a71","server":"demo"}
refused case=wrong_password reason="wrong key, or the message was changed"
refused case=one_character_changed reason="wrong key, or the message was changed"
refused case=not_sealed reason="not a sealed message" The picture is a real run on a MetaTrader 4 demo account.
| Call | What it does |
|---|---|
| AesHmacKeyFromPassword(password, salt, iterations, key) | Make a 32-byte key from a password; use one salt per program |
| AesHmacSeal(key, text, sealed), AesHmacOpen(key, sealed, text) | Seal a text into base64 and open it again |
| AesHmacSealBytes(key, plain, sealed), AesHmacOpenBytes(key, sealed, plain) | The same for bytes; a sealed message is 48 bytes longer than the plain one |
| AesHmacErrorText(code) | Describe an error code |
| AesHmacEncryptBlock(key, plain, cipher), AesHmacCtr(key, counter, source, output) | One AES-256 block, and AES-256 in CTR mode (the same call decrypts) |
| AesHmacMac(key, data, mac), AesHmacPbkdf2(password, salt, iterations, size, key), AesHmacSha256(data, hash) | HMAC-SHA256, PBKDF2-HMAC-SHA256 and SHA-256 |
| AesHmacFromHex(hex, bytes), AesHmacToHex(bytes), AesHmacUtf8(text, bytes) | Hex and UTF-8 helpers |
A key compiled into a program keeps out other programs and edited files, not someone who takes the program apart.
TesterSafeLots - risk lot size that stays correct in the MT4 Strategy Tester
Risk-based lot size for MQL4 that does not depend on MODE_TICKVALUE in the Strategy Tester, so backtests of USDJPY, USDCHF or USDCAD on a USD account risk what the input says. Includes a test EA that measures the error.
Hotkeys: keyboard shortcuts for MQL4 experts and indicators, set in the inputs
One include file that gives your EA or indicator keyboard shortcuts the user types into the inputs, such as Shift+B. Each press fires one action, Shift+B never fires on a plain B, and a shortcut that cannot work is refused at start with the reason.
Three Colors
Example: Moving Average indicator filling by different colors
MACD Sample
Classical MACD Sample.