2

I have an API Integration which expects a HMAC-SHA256 signature. Example:

message = 'testmessage'
secret key = '123456789'
signature: 9cba4d1d75689509208a97b1ca42f786a630a891e2410bfb11a84feb7a4807ad

How can I generate the same signature in Apex? I tried the generateMac function from the Crypto class but it gives a different value than the one expected. Below is the code:

Blob blobSignature = Crypto.generateMac('HmacSHA256', Blob.valueOf('testmessage'), Blob.valueOf('123456789'));
System.debug(EncodingUtil.base64Encode(blobSignature));

Output: nLpNHXVolQkgipexykL3hqYwqJHiQQv7EahP63pIB60=

What I am doing incorrectly here?

1 Answer 1

4

You are encoding it wrong. Try below code:

Blob blobSignature = Crypto.generateMac('hmacSHA256', Blob.valueOf('testmessage'), Blob.valueOf('123456789'));
System.debug(EncodingUtil.convertToHex(blobSignature));

output

17:42:10:023 USER_DEBUG [56]|DEBUG|9cba4d1d75689509208a97b1ca42f786a630a891e2410bfb11a84feb7a4807ad
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .