As I continue working on my SAPUI5 project, I have encountered a challenge with creating a HMAC encoded string using the following code snippet:
var secretKey = CryptoJS.enc.Hex.parse('SECRETKEY'); //Utilizing the CRYPTOJS library!
var hash = CryptoJS.HmacSHA256('abc', secretKey);
hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
This results in
eZdbNMwgWKOANEiozokNG2FGfzI7Yy/B8IQKXr3+krY=
While implementing this logic in UI5 with the CryptoJS library, everything seems to be working fine. However, when attempting the same process in ABAP, I'm facing an issue where the HMAC encoded string is incorrect. Upon further investigation, it appears that the encoding (in ABAP) before calculating the HMAC is not correct.
I have been searching for a function module that mimics 'CryptoJS.enc.Hex.parse()', which interprets the parameter as encoded and converts it into a word array:
DATA:
lv_sign_key_x TYPE xstring,
lv_hmac_result TYPE string.
DATA(lv_binary_secret) = cl_abap_hmac=>string_to_xstring('SECRETKEY').
cl_abap_hmac=>calculate_hmac_for_char(
EXPORTING
if_algorithm = 'SHA256' "Hash Algorithm
if_key = lv_binary_secret "HMAC Key
if_data = 'abc' "Data
IMPORTING
ef_hmacb64string = lv_hmac_result "HMAC value as base64-encoded string
).
Which ultimately produces
9dyEZn5G+uiRwsNqgY5S6k9/gmCheFNF4vFa5qBKK1w=