Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Not able to get similar result in Java as CryptoJS.HmacSHA256 produce the result in JavaScript

I am trying to write the Java code to produce the similar result as the below java script produce for result1...

var dataToSign = "message";
var secret = "secret";
var orgId = "abcd";
var secureKey = "tZvFPMrVkgQ5m5jj";
var result1 = orgId+ CryptoJS.HmacSHA256(dataToSign, secret);
console.log( result1)

value of result1 is: abcd8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b

Now i want to generate the same result using java code..

String  orgId = "abcd";
        String key="secret";
        String dataToSign = "message";
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        String result2=orgId+java.util.Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(dataToSign.getBytes("UTF-8")));
System.out.println(result2);

I am getting value of result2 is : abcd[B@39538f9e i19IcCmVwVmMVz2x4hhmqbgl1KeU0WnXBgoDYFeWNgs=

but I want result2 similar to result1;

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should print the hex numbers, not the base64 encoded string.

Have a look at this, then use bytesToHex:

byte[] result2 = sha256_HMAC.doFinal(dataToSign.getBytes("UTF-8"));
System.out.println(orgId + bytesToHex(result2));
about 4 years ago · Juan Pablo Isaza Report

0

Solution 1: You should this line

String result2=orgId+java.util.Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(dataToSign.getBytes("UTF-8")));

To

String result2 = orgId + getHex(sha256_HMAC.doFinal(dataToSign.getBytes("UTF-8")));

Function getHex as below

static final String HEXES = "0123456789ABCDEF";
public static String getHex(byte[] raw) {
    if (raw == null) {
        return null;
    }
    final StringBuilder hex = new StringBuilder(2 * raw.length);
    for (final byte b : raw) {
        hex.append(HEXES.charAt((b & 0xF0) >> 4))
                .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
}

Solution 2: You should this line

var result1 = orgId+ CryptoJS.HmacSHA256(dataToSign, secret);

To

var result1 = orgId+ CryptoJS.HmacSHA256(dataToSign, secret);
var result2  = CryptoJS.enc.Base64.stringify(result1)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!