+ Start a Discussion
usersfdc21usersfdc21 

How to create a HmacSHA256 hash in Salesforce to match external service

Hi guys,

I have created Rest API to integrate Salesforce with banking system.
I am facing problem with HmacSha256 encryption. I am using as below:

Blob blobSign = Crypto.generateMac('Hmac-SHA256', Blob.valueOf(privKey + xmlOutput.length() + unixTime));
String HMACKey = EncodingUtil.base64Encode(blobSign);

I am getting error 403.

Any one know how to connect external system. 

Thanks in advance.
gyani19901.3956550919266765E12gyani19901.3956550919266765E12
Hi UserSf,

Please use the below code for your problem.
 
Blob hash = Crypto.generateDigest('MD5', targetBlob);
        string payload_md5 = EncodingUtil.base64Encode(hash);
        String salt = payload_md5+',/api/salesforce/xml';
        String key = 'rc!&x1_s=-ve-vlcr&h_!t8n^3ar$+8b3ynhfo9v^)dw1+c5xh';        
        system.debug('******key******'+key);
        Blob data = crypto.generateMac('HmacSHA256',Blob.valueOf(salt), Blob.valueOf(key));
        string signature  = EncodingUtil.base64Encode(data);

Thanks
Gyani
http://www.mirketa.com
usersfdc21usersfdc21
Hi Gyani,

Thank you very much for replying me.

I tried it but unable to connect to external system. can I have your email Id ?
usersfdc21usersfdc21
Hi Gyani,

I am still facing 403 error for the below code:
       
       DateTime dateTimeNow = dateTime.now();
        String unixTime = ''+dateTimeNow.getTime()/1000;        
        String data = '<LoanApplication><LoanId>10</LoanId><LoanReference>APP123456</LoanReference></LoanApplication>'; 

        String privKey = 'privkeyabc';
        String publicKey = 'publickeyabc';
        String responseBody;    
   
        blob blobSign = Crypto.generateDigest('SHA256', Blob.valueOf(privKey + data + unixTime));
        String HMACKey = EncodingUtil.base64Encode(blobSign);
        
        String endpoint='https://api-test.abc.com.au/abc/new/';

        HttpRequest request = new HttpRequest();

        request.setMethod('POST');
        request.setEndpoint(endpoint);
        request.setHeader('Content-Type', 'application/xml');
        request.setHeader('API-KEY',publicKey);
        request.setHeader('API-HMAC',HMACKey );
        request.setHeader('API-Timestamp',unixTime);
        request.setBody(data);        
        System.debug('@@@XML Output'+data);
        
        Http http = new Http();
        HttpResponse response = http.send(request); 
        responseBody = response.getBody();      
        System.debug('@@@Response:'+ Response);
        System.debug('@@@Response Body:'+ ResponseBody);
        System.debug('@@@time: '+unixTime);
        System.debug('@@@HMACKey: '+ HMACKey);
        System.debug('@@@BlobSign: ' + blobSign);
        System.debug('@@@Data Length: ' + data.Length()); 
 
usersfdc21usersfdc21
Finally got success with creating the XML structure with mandatory fields.
sivaextsivaext
Hi Srikanth, 

I have same probelm. can you please share code to create hmacSHA256?

Thanks in Advance.