CryptEncrypt example doesn't return proper values

 

I have CTRL+C the example from https://docs.mql4.com/common/cryptencode and just changed encode to CRYPT_HASH_SHA256

but the EA returns hash:

2018.06.06 11:33:23.602 !BINA_TC_000_test AUDCAD,H1: Encoded data: size=32 26B54A596255364BF3B1D9B4DF2FD33D61974D1F000DF23B6F7DE4313A6C72E5

according to this 2 websites:

https://hash.online-convert.com/sha256-generator

https://www.freeformatter.com/hmac-generator.html#ad-output

the string "The quick brown fox jumps over the lazy dog" encoded with key "ABCDEFG" with SHA256 method is: 37E35181BA93EC243CE807651A476D5304C5DED808997B01885C91CC4BEF303B

What am I doing wrong?


whole MQL4 code that you can paste as expert advisor:

#property copyright "Copyright 2018,Grzegorz Korycki"
#property version   "1.00"
#property strict

int first_run = 1;

string ArrayToHex(uchar &arr[],int count=-1)
  {
   string res="";
//--- check
   if(count<0 || count>ArraySize(arr))
      count=ArraySize(arr);
//--- transform to HEX string
   for(int i=0; i<count; i++)
      res+=StringFormat("%.2X",arr[i]);
//---
   return(res);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void Send2()
  {
   string text="The quick brown fox jumps over the lazy dog";
   string keystr="ABCDEFG";
   uchar src[],dst[],key[];
//--- prepare key
   StringToCharArray(keystr,key);
//--- copy text to source array src[]
   StringToCharArray(text,src);
//--- print initial data
   PrintFormat("Initial data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));
//--- encrypt src[] with DES 56-bit key in key[]
   int res=CryptEncode(CRYPT_HASH_SHA256,src,key,dst);
//--- check error
   if(res>0)
     {
      //--- print encrypted data
      PrintFormat("Encoded data: size=%d %s",res,ArrayToHex(dst));
      //--- decode dst[] to src[]
     }
   else
      Print("Error in CryptEncode. Error code=",GetLastError());
  }

int OnInit()
  {
//--- create timer
   EventSetTimer(1);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   if(first_run==1) {
      first_run = 0;
      Send2();
   }
   
  }
//+------------------------------------------------------------------+


CryptEncode - Common Functions - MQL4 Reference
CryptEncode - Common Functions - MQL4 Reference
  • docs.mql4.com
//| ArrayToHex                                                       | //| Script program start function                                    |
 
Grzegorz Korycki:

I have CTRL+C the example from https://docs.mql4.com/common/cryptencode and just changed encode to CRYPT_HASH_SHA256

...

What am I doing wrong?

What you did wrong is to copy and paste code and changing it without understanding it ;-)

More seriously, the code you posted is NOT giving you an HMAC (see also here and here).. It gives you an hash, which doesn't need a password. So it gives the same results, whatever the key is.

2018.06.06 12:40:02.769    251371 EURUSD,M1: Initial data: size=44, string='The quick brown fox jumps over the lazy dog'
2018.06.06 12:40:02.769    251371 EURUSD,M1: Encoded data: size=32 26B54A596255364BF3B1D9B4DF2FD33D61974D1F000DF23B6F7DE4313A6C72E5 with key=craboutcha
...

2018.06.06 12:40:15.691    251371 EURUSD,M1: Initial data: size=44, string='The quick brown fox jumps over the lazy dog'
2018.06.06 12:40:15.691    251371 EURUSD,M1: Encoded data: size=32 26B54A596255364BF3B1D9B4DF2FD33D61974D1F000DF23B6F7DE4313A6C72E5 with key=ABCDEFG

Secondly the above results are different wrong because you need to take into account that a string in mql is null terminated. The length of your text string is 43, not 44.

//--- copy text to source array src[]
   StringToCharArray(text,src,0,StringLen(text));
 
Alain Verleyen:

What you did wrong is to copy and paste code and changing it without understanding it ;-)

More seriously, the code you posted is NOT giving you an HMAC (see also here and here).. It gives you an hash, which doesn't need a password. So it gives the same results, whatever the key is.

2018.06.06 12:40:02.769    251371 EURUSD,M1: Initial data: size=44, string='The quick brown fox jumps over the lazy dog'
2018.06.06 12:40:02.769    251371 EURUSD,M1: Encoded data: size=32 26B54A596255364BF3B1D9B4DF2FD33D61974D1F000DF23B6F7DE4313A6C72E5 with key=craboutcha
...

2018.06.06 12:40:15.691    251371 EURUSD,M1: Initial data: size=44, string='The quick brown fox jumps over the lazy dog'
2018.06.06 12:40:15.691    251371 EURUSD,M1: Encoded data: size=32 26B54A596255364BF3B1D9B4DF2FD33D61974D1F000DF23B6F7DE4313A6C72E5 with key=ABCDEFG

Secondly the above results are different wrong because you need to take into account that a string in mql is null terminated. The length of your text string is 43, not 44.

Thanks. I used the Freelance part to get dev to do my sha256/384/512 functions.

Reason: