CryptEncode HMAC questions

 
let CryptoJS = require("crypto-js");
let request = require('request');

let msg = "The quick brown fox jumps over the lazy dog";
let secret = "01234567890123456789012345678901";

let hash = CryptoJS.HmacSHA256(msg, secret)

console.log(hash.toString())

let hashInBase64 = CryptoJS.enc.Base64.stringify(hash)

console.log(hashInBase64)

Javascript has libraries to create hmac sha256 and base64 encoding while mql5 is missing the hmac within the CryptEncode function. Thankfully there is a library for hmac sha here https://www.mql5.com/en/code/21065

Below is the output from the javascript code. 

3b3803c66fe6688cc3ddcc02ca7fa6fd940a326cefbd1e3606c42eccbb21bdba (string hmac sha256)
OzgDxm/maIzD3cwCyn+m/ZQKMmzvvR42BsQuzLshvbo= (string base64)

Now I am trying to duplicate the output within mql5... HMAC sha256 to string is simple thanks to the library. 

void code1(){
   Print("CODE1");
   const string text="The quick brown fox jumps over the lazy dog";
   const string keystr="01234567890123456789012345678901";
   Print("MQL HMACsha256=" +  hash.hmac(text,keystr) );
}

This will output: MQL HMACsha256=3b3803c66fe6688cc3ddcc02ca7fa6fd940a326cefbd1e3606c42eccbb21bdba


void code2(){
   Print("CODE2");
   string text="The quick brown fox jumps over the lazy dog";
   string keystr="01234567890123456789012345678901";
   string hmacstr = hash.hmac(text,keystr);
   uchar str[],key[],dst[];
   StringToCharArray(hmacstr,str,0,StringLen(hmacstr) );
   int res = CryptEncode(CRYPT_BASE64,str,key,dst);
   if(res>0){
   Print("MQL BASE64="+ CharArrayToString(dst,0,WHOLE_ARRAY,CP_UTF8));
   }
}

Output: MQL BASE64=M2IzODAzYzY2ZmU2Njg4Y2MzZGRjYzAyY2E3ZmE2ZmQ5NDBhMzI2Y2VmYmQxZTM2MDZjNDJlY2NiYjIxYmRiYQ==


Well that's not right. What could have been the issue? If you take the HMAC sha256 string into any online base64 encoder you will have this output because we're taking a string of the hmac sha256 and base64 encoding it. 



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.
 
TradingToolCrypto:

Javascript has libraries to create hmac sha256 and base64 encoding while mql5 is missing the hmac within the CryptEncode function. Thankfully there is a library for hmac sha here https://www.mql5.com/en/code/21065

Below is the output from the javascript code. 

Now I am trying to duplicate the output within mql5... HMAC sha256 to string is simple thanks to the library. 

This will output: MQL HMACsha256=3b3803c66fe6688cc3ddcc02ca7fa6fd940a326cefbd1e3606c42eccbb21bdba


Output: MQL BASE64=M2IzODAzYzY2ZmU2Njg4Y2MzZGRjYzAyY2E3ZmE2ZmQ5NDBhMzI2Y2VmYmQxZTM2MDZjNDJlY2NiYjIxYmRiYQ==


Well that's not right. What could have been the issue? If you take the HMAC sha256 string into any online base64 encoder you will have this output because we're taking a string of the hmac sha256 and base64 encoding it. 




I have tried to verify your issue, but when using this encoder/decoder, everything seems to line up correctly.

https://www.base64decode.net/

Please verify the output from javascript, as it seems to be an encoding issue somewhere with the charset in use.

Base64 Decode - Online Tool
  • www.base64decode.net
Decode and Encode Base64 data with this online base64 decoder.
 
Difference between CryptoJS.enc.Base64.stringify() and normal Base64 encryption
Difference between CryptoJS.enc.Base64.stringify() and normal Base64 encryption
  • 2015.08.24
  • Bobface Bobface 2,402 4 4 gold badges 17 17 silver badges 52 52 bronze badges
  • stackoverflow.com
I'm trying to encrypt the following hash to base64: It's needed to access the API of a website. The website shows an example script in JavaScript using the method to encrypt the hash. The result with this method is However, every online base64 encryption tool I tried gives me the following result: I need to create the...
 

sorry to bring up a 2 years old topic again. I didn't understand at last  what was the point?  is the mql crypt encode function working correctly ? and the result differences are not important what we see at print logs?   please explain more. thanks in advanced.

 int res = CryptEncode(CRYPT_BASE64,str,key,dst);


edit 1: 

thanks to that post in stackoverflow , after multiple hours of trial and reading it over and over again. I finally understood what he wrote. 

when we use that sha256 library , the returned value is in hex format not  decimal or utf normal strings.

so I wrote a function for finding hex characters and then when I had an array with hex data, I used it on cryptencode as src array, and an empty array as key, and another array for base64encoded results.   

and finally it works :D 

Reason: