Guarda come scaricare robot di trading gratuitamente
Ci trovi su Facebook!
Unisciti alla nostra fan page
Script interessante?
Pubblica il link!
lasciare che altri lo valutino
Ti è piaciuto lo script? Provalo nel Terminale MetaTrader 5
Librerie

AesHmac: AES-256 encryption with tamper check and password keys for MQL5 - libreria per MetaTrader 5

Visualizzazioni:
68
Valutazioni:
(1)
Pubblicato:
aeshmac-output.png (57.95 KB)
\MQL5\Include\AesHmac\
AesHmac.mqh (21.99 KB) visualizza
Freelance MQL5 Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance

AesHmac encrypts texts and bytes in MQL5 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>

Features

  • AES-256 in CTR mode with a fresh random nonce for every message, so sealing the same text twice gives two different results.
  • An HMAC-SHA256 tag over the nonce and the encrypted text, checked over its full length before anything is decrypted.
  • Separate keys for encrypting and for the tag, both derived from the key you give.
  • Keys from a password with PBKDF2-HMAC-SHA256: 10000 rounds by default, which took 14 ms in the run in the picture.
  • Text goes in and comes out as UTF-8, so any language survives. The sealed text is base64, safe in a file, a global variable or a URL parameter. Bytes work too.
  • Checked against official test vectors: NIST SP 800-38A for AES-256 (block and CTR mode), all seven cases of RFC 4231 for HMAC-SHA256 and RFC 7914 for PBKDF2. The example script runs a self test against them first.
  • A message sealed by Python's cryptography package with the same steps opens in MQL5, and the other way round.
  • Every call that can fail returns AES_HMAC_OK or an error code, and AesHmacErrorText says what it means. The library never prints anything by itself.

Example

#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 MQL5\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=14
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 demo account.

The Experts log of the example: self test, key, sealed text, the file opened and the three refusals

API

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.

History Gaps: find missing bars before you backtest History Gaps: find missing bars before you backtest

Checks the chart's history against the symbol's trading sessions and separates real holes from holiday short sessions and days without bars. Marks the holes on the chart, shows coverage and the largest hole, warns when the history stops short of now, and can write a CSV report.

Prop Firm Equity Guard daily loss and max drawdown protector Prop Firm Equity Guard daily loss and max drawdown protector

Prop Firm Equity Guard protects prop-firm challenges and funded accounts. It monitors live equity against your daily loss and max drawdown limits, then closes all positions, deletes pending orders and locks trading before the firm's limit is hit. A safety buffer makes it act early, and anything opened while locked is closed instantly. It supports static and trailing drawdown, an optional profit target lock and alerts, saves its state through restarts, and shows an on-chart dashboard with room left and a reset countdown. It works alongside manual trading and other EAs.

Structural Swing Migration BUY Structural Swing Migration BUY

Structural Swing Migration BUY is an educational Expert Advisor for MetaTrader 5 that opens BUY positions when confirmed swing highs and swing lows move upward together. It evaluates closed M15 candles and combines structural confirmation with a volatility threshold and a bullish candle filter.

Demetra Symbol Spec Sheet Demetra Symbol Spec Sheet

Puts the contract specification of the chart symbol on one sheet, including the money value of one point and the margin for one lot.