I am trying to do an HMAC-SHA256 signature calculation in apex , but couldn't find clear instructions from the service provider on how to generate a request string for creating an HMAC-Signature.
But found below rails code on the vendor site showing how to generate the request string.
Can someone with rails hands on can help on showing how the exact output of below variables will look like
sorted_key_values request_string
By seeing the proper output of these I can analyze on how to implement the same in the apex. Sample service request payload:
{
"to_account":"Example user",
"token":"5TH3ACC3AU21",
"transaction_reference":"",
"from_account":"First1 Last1",
"from_fund":"THE TORONTO-DOMINION BANK",
"transaction_type":"send_money",
"amount_in_cents":500,
"type":"transaction",
"created_by_user":"699cMPe6BAyqvVsZA5mo",
"message":"",
"state":"completed",
"link_url":"",
"email":"[email protected]",
"signature":"USYpBfZFQQHa2%2BT6UtDPUVFfUPP0aobWpXe5DE9hPOY%3D%0A"
}
Sample HMAC-SHA256 signature calculation - using Rails/Sinatra code
require 'openssl'
require 'base64'
require 'erb'
webhook_url = 'http://your.domain.com/your/webhook/url';
webhook_signing_key = 'v7aJHjbbxASKiwDW5wq6';
original_signature = params.delete(:signature)
sorted_key_values = params.keys.sort.map{ |key| "#{key}#{params[key]}" }.join
// looking to see how the output of sorted_key_values variable will look like
request_string = "POST\n#{webhook_url}\n#{sorted_key_values}"
// looking to see how the final output of request_string variable will look like