1

I wrote a Java program which generates HMAC SHA hash code, But due to some reason I have to write the same code in NodeJs/JavaScript. I tried googling around but did not get anything. In this Java code, I am passing URI and Key as arguments, to generate the hash code, where URI contains Timestamp. The java code is as :

    public static String calcMAC(String data, byte[] key) throws Exception {
    String result=null;
    SecretKeySpec signKey = new SecretKeySpec(key, SecurityConstants.HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(SecurityConstants.HMAC_SHA1_ALGORITHM);
    mac.init(signKey);
    byte[] rawHmac;
    try {
        rawHmac = mac.doFinal(data.getBytes("US-ASCII"));
        result = Base64.encodeBase64String(rawHmac);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result.trim();
}

public static void main(String args[]) {
    String timestamp = args[0];
    String key = "d134hjeefcgkahvg32ajkdbaff84ff180";
    String out = null;
    try {
        out = calcMAC("/req?app_id=47ca34" + timestamp + "=2018-05-22T12:02:15Z", 
                key.getBytes());
        System.out.println(URLEncoder.encode(out, "UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Is it possible to achieve the same goal in NodeJs/JavaScript?

Note:: I have to call this script from Postman pre-request script.

2

1 Answer 1

2

The crypto module should do this for you, you can substitute the 'data' variable with whatever you want to hash:

const crypto = require('crypto');

const data = 'The fault dear Brutus lies not in our stars';
const key = Buffer.from('d134hjeefcgkahvg32ajkdbaff84ff180', 'utf8');

const hash = crypto.createHmac('sha1', key).update(data).digest('base64');
const uriEncodedHash = encodeURIComponent(hash);
console.log('Hash: ' + uriEncodedHash);

Hashing the data in both Java and Node.js gives me the result (URI Encoded) of:

TJJ3xj93m8bfVpGoucluMQqkB0o%3D

The same Java code would be:

public static void main(String args[]) {
    String data = "The fault dear Brutus lies not in our stars";
    String key = "d134hjeefcgkahvg32ajkdbaff84ff180";
    String out = null;
    try {
        out = calcMAC(data, key.getBytes());
        System.out.println(URLEncoder.encode(out, "UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Again, we can put anything into 'data' we want.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you log what data you're getting in the server when you post using postman?
There was an error in evaluating the Pre-request Script: Error: Cannot find module 'crypto'. I am new to Postman.
This script will only work on the server side, run by Node.js, if you run it outside this context it won't work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.