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?
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
- DevGlan
- www.devglan.com
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
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
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!

- www.mql5.com
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