Hi all,
Currently on my SAPUI5 project, I am creating a HMAC encoded string with this line of code;
var secretKey = CryptoJS.enc.Hex.parse('SECRETKEY'); //USING THE CRYPTOJS LIBRARY!
var hash = CryptoJS.HmacSHA256('abc', secretKey);
hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
I am using the CryptoJS library to execute this code in UI5.
However the problem is that I am receiving the wrong HMAC encoded string when I want to do the same in ABAP. After testing a few times, it seems like the encoding (in abap) is wrong before the HMAC is calculated.
Is there a function module that does 'CryptoJS.enc.Hex.parse()' - after googling what it does it 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
).
Thank you in advance.
Request clarification before answering.
Hi, Sunil,
If string is already hex-encoded, you can do a direct conversion:
DATA lv_binary_secret TYPE xstring.
DATA lv_input TYPE string VALUE 'EFBBBF'.
"only uppercase, translate to upper case if needed
lv_binary_secret = lv_input.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Victor, The problem is that it needs to be hex encoded the same as the javascript version which is why they are bringing back two different output values.
Using 'SECRETKEY' as the key and 'abc' as the data...
Javascript is giving me 'eZdbNMwgWKOANEiozokNG2FGfzI7Yy/B8IQKXr3+krY=' whilst the ABAP code is giving me '9dyEZn5G+uiRwsNqgY5S6k9/gmCheFNF4vFa5qBKK1w='
| User | Count |
|---|---|
| 10 | |
| 5 | |
| 5 | |
| 5 | |
| 4 | |
| 2 | |
| 2 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.