function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
abdnabdn 

Using a Hex key to create SHA-256 MAC .

Hi all...

I want to create SHA-256 HMAC encoded message. The secrete key which i am using is in HEX formate. On a website i converted my key '1234567890abcdef1a2b3c4d5f6a7b3a' to characters and used in the code below.  I am also using an online tool which computes the  SHA-256 HMAC message so i can check my code is working properly or not. 

Problem:-  My code does not create correct message(only first few characters matches some time with the online tool messae). When i traced the error i found that there is some problem with Blob.valueOf(key) at line 4 because when at line 6 i convert the key back to Hex the output does not match the Hex input (Output=12345678c290c2abc38dc3af1a2b3c4d5f6a7b3a) red colored charecters are extra and e is missing. I tried some other hex charactres and all the time extra characters appeared in the hex string when converted back(mostly c2 and 38 are the extra characters).  I also tried to convert Hex to decimal in apex and then by using this method String.fromCharArray( intArr ) converted decimals into string, again the result was same(output 2  = 12345678c290c2abc38dc3af1a2b3c4d5f6a7b3a). Here is the code:

/*1234567890abcdef1a2b3c4d5f6a7b3a This hex is converted to the following character String 'key'*/
String key = '4Vx�«Íï+<M_j{:';    
String temp = '1F1F1F1F1356998400';
Blob mac = Crypto.generateMac('hmacSHA256',Blob.valueOf(temp),Blob.valueOf(key));
String csvBody = EncodingUtil.convertToHex(mac);
system.debug(EncodingUtil.convertToHex(Blob.valueOf(key))); //12345678c290c2abc38dc3af1a2b3c4d5f6a7b3a

 can any one help me eith this?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You're trying to stuff a binary string into a UTF-8 string. It doesn't work, because some of the binary code points you specified are invalid in UTF-8, and so the system has to add encoding bytes. That's where the c2, etc come in to play. Instead, you need a pure blob (binary large object) representation of the key, not a UTF-8 string.

 

Try this instead:

 

String key = 'EjRWeJCrze8aKzxNX2p7Og==';
String temp = '1F1F1F1F1356998400';
Blob mac = Crypto.generateMac('hmacSHA256',Blob.valueOf(temp),EncodingUtil.base64Decode(key));
String csvBody = EncodingUtil.convertToHex(mac);
System.debug(LoggingLevel.Error,EncodingUtil.convertToHex(EncodingUtil.base64Decode(key)));

 

 

All Answers

sfdcfoxsfdcfox

You're trying to stuff a binary string into a UTF-8 string. It doesn't work, because some of the binary code points you specified are invalid in UTF-8, and so the system has to add encoding bytes. That's where the c2, etc come in to play. Instead, you need a pure blob (binary large object) representation of the key, not a UTF-8 string.

 

Try this instead:

 

String key = 'EjRWeJCrze8aKzxNX2p7Og==';
String temp = '1F1F1F1F1356998400';
Blob mac = Crypto.generateMac('hmacSHA256',Blob.valueOf(temp),EncodingUtil.base64Decode(key));
String csvBody = EncodingUtil.convertToHex(mac);
System.debug(LoggingLevel.Error,EncodingUtil.convertToHex(EncodingUtil.base64Decode(key)));

 

 

This was selected as the best answer
abdnabdn

thank you sfdcfox! this worked for me. 

 

I wrote a method for HEX to Base64 conversion. after coonversion i am using your method and it is working fine. thank you again.