Discussing the article: "Implementing the SHA-256 Cryptographic Algorithm from Scratch in MQL5"

 

Check out the new article: Implementing the SHA-256 Cryptographic Algorithm from Scratch in MQL5.

Building DLL-free cryptocurrency exchange integrations has long been a challenge, but this solution provides a complete framework for direct market connectivity.

When implementing SHA-256 in a trading environment, performance optimization becomes crucial because every millisecond can impact trading outcomes. A custom implementation offers several avenues for optimization that wouldn't be possible with built-in functions.

Trading systems often exhibit specific patterns in their cryptographic operations. For instance, order signatures typically contain similar components like timestamps, symbols, and quantities. By understanding these patterns, we can optimize our SHA-256 implementation specifically for trading-related data structures.

Consider how order placement works in a typical trading scenario. Each order requires multiple pieces of information: the trading pair, order type, quantity, price, and timestamp. In standard implementations, this data would be processed as a completely new input each time. However, we can optimize this process by recognizing that many components remain constant or follow predictable patterns.

Author: Abdulkudus Okatengwu Kadir

 

If key_text is more than 64 characters, then HMacSha256 calculates incorrectly. What should be corrected for this?

 
Sergey Zhilinskiy #:

If key_text is more than 64 characters, then HMacSha256 calculates incorrectly. What should be corrected for this?

My current implementation adapts to keys with more than 64 characters length. Do you have any specific key,message pair for which it did not work?

 
Abdulkudus Okatengwu Kadir #:

My current implementation adapts to keys with more than 64 characters length. Do you have any specific key,message pair for which it did not work?

string text = "Hello";

string key_text = "1234567890123456789012345678901234567890123456789012345678901234";   

https://www.devglan.com/online-tools/hmac-sha256-online -> 7558a77ff19ed6cb4777355e4bbc4772759a8130e1bb0913ba62b88411fdbaf8

Test script -> 2025.02.27 22:28:43.792 Sha256TestFile (EURUSD,M5) 6d8ee9dc1d16261fd986fafb97d919584aa206ca76706fb3deccc63ab2b7f6b

if  string key_text = "123456789012345678901234567890123456789012345678901234567890123" - OK   
HMAC-SHA256 Hash Generator | Devglan
  • DevGlan
  • www.devglan.com
HMAC is a message authentication code (MAC) using a hash function. It combines with any cryptographic hash function, for example, md5, sha1, sha256. Hash function is wrapped to a class as one template parameter in HMAC and the wrapper class only has a static function involving...
 
Sergey Zhilinskiy #:

string text = "Hello";

string key_text = "1234567890123456789012345678901234567890123456789012345678901234";   

https://www.devglan.com/online-tools/hmac-sha256-online -> 7558a77ff19ed6cb4777355e4bbc4772759a8130e1bb0913ba62b88411fdbaf8

Test script -> 2025.02.27 22:28:43.792 Sha256TestFile (EURUSD,M5) 6d8ee9dc1d16261fd986fafb97d919584aa206ca76706fb3deccc63ab2b7f6b

if  string key_text = "123456789012345678901234567890123456789012345678901234567890123" - OK   

I just tried it on my terminal and got same as the online hash tool:

2025.02.28 12:37:16.468 hashin_example_code (EURUSD,M5) 7558A77FF19ED6CB4777355E4BBC4772759A8130E1BB0913BA62B88411FDBAF8

using the code below:
void Hash()
{
   // The text to hash
   string text = "Hello";
   string key_text = "1234567890123456789012345678901234567890123456789012345678901234";
   HMacSha256 myhash(key_text, text);
   Print(myhash.hexval);
}
You might want to share your code.

 

Yes, it works correctly with the original Sha256Algorithm.mqh. I made some changes, maybe that's why it didn't work?

 string CSha256Class::GetHex( void )
  {
   string result  = "" ;
 /*
   result += UintToHex(h0);
   result += UintToHex(h1);
   result += UintToHex(h2);
   result += UintToHex(h3);
   result += UintToHex(h4);
   result += UintToHex(h5);
   result += UintToHex(h6);
   result += UintToHex(h7);
*/ 
   result += StringFormat ( "%.2x" ,h0);
   result += StringFormat ( "%.2x" ,h1);
   result += StringFormat ( "%.2x" ,h2);
   result += StringFormat ( "%.2x" ,h3);
   result += StringFormat ( "%.2x" ,h4);
   result += StringFormat ( "%.2x" ,h5);
   result += StringFormat ( "%.2x" ,h6);
   result += StringFormat ( "%.2x" ,h7);

   return (result);
  }

Sorry to bother you!

 
There is an implementation of SHA256, SHA384, SHA512 in the codebase, it works in MQL5 as well.
SHA256, SHA384 and SHA512 + HMAC
SHA256, SHA384 and SHA512 + HMAC
  • www.mql5.com
Many developers need these functions, while working with different kinds of external APIs, like Bitcoin and altcoin exchanges where it is often necessary to send data with the confirmation of parameters validity through HMAC-SHA512, HMAC-SHA384 and HMAC-SHA256.