文章 "从头开始以 MQL5 实现 SHA-256 加密算法"

 

新文章 从头开始以 MQL5 实现 SHA-256 加密算法已发布:

长期以来,构建无 DLL 的加密货币兑换集成一直是一个挑战,但该解决方案为直接市场对接提供了一个完整的框架。

当在交易环境中实现 SHA-256 时,性能优化变得至关重要,因为每一毫秒都会影响交易成果。自定义实现提供了若干种优化道路,而内置函数不能做到。

交易系统在其加密操作中往往展现出特定的范式。举例,订单签名典型情况下包含类似的组件,例如时间戳、品种、和数量。通过了解这些范式,我们就能针对与交易相关的数据结构专门优化 SHA-256 实现。

研究在典型交易场景中下单如何运作。每笔订单都需要多个信息片:交易对、订单类型、数量、价格、和时间戳。在标准实现中,每次都会将该数据作为全新的输入进行处理。然而,我们能够优化这一过程,即识别许多组件维持恒定,或遵循可预测的范式。


作者:Abdulkudus Okatengwu Kadir

 

如果 key_text 超过 64 个字符,则 HMacSha256 计算错误。应该如何纠正?

 
Sergey Zhilinskiy #:

如果 key_text 超过 64 个字符,则 HMacSha256 计算错误。应该如何纠正?

我目前的实现可以适应长度超过 64 个字符的密钥。您是否有任何特定的密钥和信息对无法正常工作?

 
Abdulkudus Okatengwu Kadir #:

我目前的实现可适应长度超过 64 个字符的密钥。您是否有任何特定的密钥和信息对无法工作?

字符串 text = "你好";

string key_text = "12345678901234567890123456789012345678901234567890123456789012345678901234";

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

测试脚本 -> 2025.02.27 22:28:43.792 Sha256TestFile (EURUSD,M5) 6d8ee9dc1d16261fd986fafb97d919584aa206ca76706fb3deccc63ab2b7f6b

ifstring key_text = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123" - 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 #:

字符串 text = "你好";

string key_text = "12345678901234567890123456789012345678901234567890123456789012345678901234";

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

测试脚本 -> 2025.02.27 22:28:43.792 Sha256TestFile (EURUSD,M5) 6d8ee9dc1d16261fd986fafb97d919584aa206ca76706fb3deccc63ab2b7f6b

ifstring key_text = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123" - OK

我在终端上试了一下,结果与在线哈希工具相同:

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

使用下面的代码:
void Hash()
{
   // 要散列的文本
   string text = "Hello";
   string key_text = "1234567890123456789012345678901234567890123456789012345678901234";
   HMacSha256 myhash(key_text, text);
   Print(myhash.hexval);
}
您可能需要分享您的代码。

 

是的,使用原始的 Sha256Algorithm.mqh 可以正常工作。我做了一些改动,也许这就是它不能工作的原因?

 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);
  }

很抱歉打扰您!

 
kodobase 中有 SHA256、SHA384 和 SHA512 的 实现,在 MQL5 中也同样有效。
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.