Page 1 of 1

Line Feeds in Strings to be Hashed

Posted: Fri Mar 21, 2025 5:21 pm
by jan
Good day!

I am working on consuming some REST APIs and am having trouble getting the hashing algorithm to work properly when Line Feeds are involved.

The hashing uses SHA-256 HMAC with the output encoding as Base64. This is the string in the sample provided by the REST API documentation.
host: apitest.cybersource.com
date: Fri, 21 Mar 2025 04:32:08 GMT
request-target: post /pts/v2/payments/
digest: SHA-256=ZWTHRbrcpmA5sUP4W5BjzdwIcMzVxcBGIPtL5KdoWNE=
v-c-merchant-id: testrest
The key from their sample is also using Base64 encoding.
yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE=
I used the website https://emn178.github.io/online-tools/sha256.html to test this and it matches with what is in the API documentation's results. The hashed value provided by the website and the sample in the API docs is below:
JFiQe35A6Sc7Vfu6YhhVi6IxefCQnz6AQHw1f4MsyYQ=
When I try this in Lansa, I keep getting different hashed values which I suspect is due to my mishandling of the Line Feeds. I then tried to test this theory by removing the LineFeeds.
host: apitest.cybersource.com date: Fri, 21 Mar 2025 04:32:08 GMT request-target: post /pts/v2/payments/ digest: SHA-256=ZWTHRbrcpmA5sUP4W5BjzdwIcMzVxcBGIPtL5KdoWNE= v-c-merchant-id: testrest
And I got this result from my Lansa Form, which also matches with results in https://emn178.github.io/online-tools/sha256.html for the same String.
7kLPdWxkZvkQWDlLBf8QyiWEcIuK2dGRGHf6XAuzrX4=
Does anyone know how to configure Strings in Lansa to behave similarly with LineFeeds? I have tried the following and nothing has worked so far:
  • Used (10).asUnicodeString at the end of each line (except the last line)
  • Appended "\n" at the end of each line (except the last line)
  • Used field types String and NVarChar

Re: Line Feeds in Strings to be Hashed

Posted: Fri Mar 21, 2025 5:49 pm
by jan
Weirdly, I got it working in a WAM using the code below.

Code: Select all

#xNoteSubject := 'host: apitest.cybersource.com' + (10).AsUnicodeString
#xNoteSubject += 'date: Fri, 21 Mar 2025 04:32:08 GMT' + (10).AsUnicodeString
#xNoteSubject += 'request-target: post /pts/v2/payments/' + (10).AsUnicodeString
#xNoteSubject += 'digest: SHA-256=ZWTHRbrcpmA5sUP4W5BjzdwIcMzVxcBGIPtL5KdoWNE=' + (10).AsUnicodeString
#xNoteSubject += 'v-c-merchant-id: testrest'
This now generates the matching Hashed value the Sample API provides.
JFiQe35A6Sc7Vfu6YhhVi6IxefCQnz6AQHw1f4MsyYQ=