ABAP string to SHA256 unlike anything in SAPUI5 or JavaScript

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=

Answer №1

Learn the precise method for encoding to hexadecimal and calculating HMAC256. Click here

data lv_binary_secret type xstring.
data lv_string type string value '48656c6c6f2c20576f726c21'.
translate lv_string to uppercase.
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
).`enter code here`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to retrieve a user's login information when their provided ID and Password match the records within the database?

When a user enters their ID and password in this form, the system checks whether it matches the data in the database. If there is a match, the user is allowed to log in and redirected to the "action.php" page. If there is no match, an error message is di ...

AngularJS: Unable to preserve the data

I'm currently working on an issue with saving updated functions using angularJS. I've managed to edit the data and update it on the database side, but the changes aren't reflecting on the frontend side unless I logout and login again. I need ...

Can inter-portlet communication be achieved using AJAX?

Is it possible to use an AJAX call in Portlet A to dynamically update Portlet B without refreshing the entire portal page? I am aware that portlets can refresh their content using the JSR286 resourceURL tag, but I am curious about targeting and updating a ...

What could be causing the JQuery date picker to fail to load on the initial ng-click event?

I am encountering an issue with a JQuery UI date picker that is loaded through ng-click using the code below: Input: <input type="text" id="datepicker" autocomplete="off" ng-model="selectedDate" ng-click="showDatepicker()" placeholder="(Click to sele ...

What are some ways to connect my CSS styles without encountering a MIME error?

Working on a Nodejs - Express - Vanillajs project, I encountered an issue when testing my routes. The index page failed to load CSS, displaying the following error message: Refused to apply style from 'http://localhost:3000/public/css/bootstrap.min. ...

Adjusting the width of the container <div> will automatically resize the inner <div> to match

I have a main container element. .wrapper{ border:1px solid blue; position:relative; top:20%; left:30%; height: 300px; width: 350px; } When I adjust the width of the parent container, the child box does not reposition itself accor ...

Browse through the options in the dropdown list -Angular 6

I am looking to implement a filtering feature in my Angular app where data can be filtered based on the selected product number from a dropdown menu. The product data is structured in JSON format as shown below. component.ts productlistArray = [{ num ...

Struggling to retrieve an element from an object received through a get request in Express

I am facing an issue with a get request that returns an array of objects retrieved from a database using Knex. I am trying to use .map to make these objects accessible, but I am struggling with accessing the final element which is a sum(). It seems like a ...

Modifications performed on the Xhtml generated from xml and XSLT should be mirrored back into the original XML document

After transforming an XML file with xsl and loading it into a browser as html, I am making the html editable using the content editable attribute of html5. How can I transform their edits back to the original xml document, even if they add new nodes to e ...

An error has occurred: Noty (notification library) is not defined in this AngularJS Web Application

I am currently diving into the world of AngularJS and building a web application from scratch. As a newbie to AngularJS, I want to point out that I might be missing something crucial. An issue has arisen: After installing the Noty library (npm install no ...

Ways to populate the second nested array with new values without overwriting existing ones

I am encountering the following issue: this.logs = {}; this.logs[1] = resp; In my initial array, I have the result shown in the image below: https://i.sstatic.net/RScSm.png However, when I try to add a second level array with another array like this: th ...

Storing chrome identity api responses in a Vue.js component can be achieved by creating a function

When passing the response from the Chrome Identity API to the tab running my Vue-powered Chrome extension, I encountered an issue in storing this information inside my Vue instance for use within a component. Despite trying to assign the info to a variable ...

"Phraseapp is in the process of refreshing the primary language document

I currently have a locale file that contains various text keys that need to be translated. Each key corresponds to a specific text that needs to be translated into different languages: { key1: "Text to translate", key2: "Another text to translate", ...

Tips for retrieving specific information from Wikipedia using AJAX

Is there a way to retrieve the information consistently displayed in the right box during searches using AJAX? I've tried using the Wikipedia API, but haven't been able to find the specific information I need. https://i.sstatic.net/wqJEc.png ...

What is the best way to guide a user to a specific page based on the choice they made after submitting a form?

My form includes a list of 6 options where users can only select one. After submitting the form, I want to redirect them to a specific page based on their selection. For example, I have the following 3 options: Facebook Youtube Twitter If a user selects ...

What is the minimum number of characters required to embed JavaScript using the HTML5 <script> element?

Is this the most efficient way to include external JavaScript on a page, now that the type attribute of the <script> tag can be omitted in HTML5? <script src="URL"></script> Is there a way to optimize this further, especially if a frame ...

"Create dynamic tables with AngularJS using ng-repeat for column-specific rendering

I have a model called Item with the following structure: {data: String, schedule: DateTime, category: String} I want to create a report that displays the data in a table format like this: <table> <tr> <th>Time Range</th&g ...

ISO format for the number of weeks in a month, considering weeks that cross over Thursday

I am looking for a precise number of weeks per month in accordance with the ISO standard. Instead of using JavaScript's Date, I am utilizing momentJS. According to the ISO date format, a month's week count is determined based on whether it cont ...

How can I dynamically assign a className in a React component based on the current state when importing styles?

I've been utilizing the style-loader to insert CSS modularly into my components ({style.exampleClassName}). My goal is to showcase a loader for a specific duration before displaying an image (at least 16 of these components in a grid layout). This i ...

Is it possible to create a column break within a CSS multicolumn design?

My text content sprawls over a CSS multicolumn layout with two, three, or four columns, utilizing CSS hyphenation. Sometimes, I want to terminate the text in one column early to allow the next paragraph to begin at the top of the next column. Is there a s ...