on ‎2019 Jul 16 12:02 PM
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.
In this case are you sure about this line in your JS code?
var secretKey = CryptoJS.enc.Hex.parse('SECRETKEY');//USING THE CRYPTOJS LIBRARY!Method CryptoJS.enc.Hex.parse parses the already Hex-encoded string and converts it into string.
So if you want string from Hex, you use it and if you want hex-encoding from string then you use Hex.stringify.
var words = CryptoJS.enc.Hex.parse("48656c6c6f2c20576f726c6421");
var hex = CryptoJS.enc.Hex.stringify(words);So I am a bit confused here.
What is a value returned by this code? I assume null.
var secretKey = CryptoJS.enc.Hex.parse('SECRETKEY');//USING THE CRYPTOJS LIBRARY!Check CryptoJS docs:
https://cryptojs.gitbook.io/docs/
Viktor
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry if it may seem confusing to you. not sure if 'SECRETKEY' is confusing you, I only used that word as I don't want to share the actual key I'll use '48656c6c6f2c20576f726c6421' as the key I am parsing for now.. The code in Javascript is perfectly correct and I need the ABAP to perform the same way/same results.
In Javascript I am parsing the hex which comes back with a word array using this line of code
var words = CryptoJS.enc.Hex.parse("48656c6c6f2c20576f726c6421");
I then use the word array to be placed in var hash = CryptoJS.HmacSHA256('abc', wordArray); and then converted into Base64
This is all correct and the documentation is in this page - https://docs.adyen.com/classic-integration/hosted-payment-pages/hmac-signature-calculation/ which is what I have done in Javascript and have tested that it works. The javascript is completely fine. I just need a solution in ABAP.
However, I want to do the same thing in ABAP... The ABAP is returning a completely different encode
DATA(lv_binary_secret)= cl_abap_hmac=>string_to_xstring('48656c6c6f2c20576f726c6421').
Relating to the link I have provided above on step 4, Do you know how to hex encode my key in ABAP?
In this case the following should work:
data lv_binary_secret type xstring.
data lv_string type string value '48656c6c6f2c20576f726c6421'.
translate lv_string to upper case.
lv_binary_secret = lv_string.
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 = data(lv_hmac_result) "HMAC value as base64-encoded string
).
| User | Count |
|---|---|
| 9 | |
| 6 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.