How to Import a Cryptographic Library for Computing HMAC-SHA256 Hash in a Node.js Workflow Action Step?

This topic was automatically generated from Slack. You can find the original thread here.

I want to run a node.js action on my workflow. It is required from me to import a cryptographic library to compute the HMAC-SHA256 hash. If I can’t do it immediately on the action’s step, could you give me any recommendation please?

You can use the crypto module, which is built into Node.js, to compute the HMAC-SHA256 hash in your Pipedream workflow. Here’s an example of how to use the crypto module to compute the HMAC-SHA256 hash in a Pipedream component: import crypto from "crypto"; export default defineComponent({ props: { secret: { type: "string", label: "Secret Key", }, message: { type: "string", label: "Message", }, }, async run({ steps, $ }) { const hmac = crypto.createHmac("sha256", this.secret); hmac.update(this.message); const hash = hmac.digest("hex"); return hash; }, }); Add this code to a Node.js code step in your workflow, and provide the required secret and message props. The step will compute the HMAC-SHA256 hash and return it as the step output.

I added the following code and it returns error message “the data argument is not defined”, while there is no data argument in my script. Could you update it to run properly?

import crypto from "crypto";

export default defineComponent({
    props: {
      payload: {type: 'string'},
      webhooksKey: {type: 'string'},
      xeroSignature: {type: 'string'}
     
    },
async run({ steps, $ }) {
    const hmac = crypto.createHmac("sha256", this.webhooksKey);
    hmac.update(this.payload);
    const hash = hmac.digest("base64");
    let isValidRequest = false;
      if (hash===this.xeroSignature){
        isValidRequest=true;
          await  $.respond ({
                status: 200,
                immediate:true
        });
      } else{
        await  $.respond({
              status: 401,
              immediate: true
        })
      }

    return hash;
    return isValidRequest;
  },
 
});

I apologize for the confusion. It seems there’s a small mistake in the import statement for the crypto module. Since crypto is a built-in Node.js module, you should use the require syntax instead of import. Here’s the updated code: const crypto = require("crypto"); export default defineComponent({ props: { payload: { type: 'string' }, webhooksKey: { type: 'string' }, xeroSignature: { type: 'string' } }, async run({ steps, $ }) { const hmac = crypto.createHmac("sha256", this.webhooksKey); hmac.update(this.payload); const hash = hmac.digest("base64"); let isValidRequest = false; if (hash === this.xeroSignature) { isValidRequest = true; await $.respond({ status: 200, immediate: true }); } else { await $.respond({ status: 401, immediate: true }); } return { hash, isValidRequest }; }, }); I’ve replaced the import statement with const crypto = require("crypto"); and combined the two return statements into a single return statement returning an object with both hash and isValidRequest properties. This should resolve the issue.

this returned me this error: “ReferenceError: require is not defined in ES module scope, you can use import instead”. do you have any suggestion ?

I apologize for the confusion earlier. Since crypto is a built-in Node.js module, you can use the import statement with the crypto module like this: ```
import crypto from “crypto”; export default defineComponent({ props: { payload: { type: ‘string’ }, webhooksKey: { type: ‘string’ }, xeroSignature: { type: ‘string’ } }, async run({ steps, $ }) { const hmac = crypto.createHmac(“sha256”, this.webhooksKey); hmac.update(this.payload); const hash = hmac.digest(“base64”); let isValidRequest = false; if (hash === this.xeroSignature) { isValidRequest = true; await $.respond({ status: 200, immediate: true }); } else { await $.respond({ status: 401, immediate: true }); } return { hash, isValidRequest }; }, });