# How to use node module "crypto" correctly?

**URL:** <https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170>\
**Category:** General\
**Created:** [14 August 2024 08:12 UTC](https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170 "2024-08-14T08:12:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [14 August 2024 08:12 UTC](https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170/1 "2024-08-14T08:12:04Z")

</div>

i want to calc a hmac with md5.

crypto-js is not longer maintained. [crypto-js - npm](https://www.npmjs.com/package/crypto-js)

Is it possible to use node:crypto in a function node?  
[https://nodejs.org/docs/latest-v20.x/api/crypto.html#class-hmac](https://nodejs.org/docs/latest-v20.x/api/crypto.html#class-hmac)

Am i overthinking or missing something?

```auto
const text = 'I love cupcakes'
const key = 'abcdeg'

const gen_hmac = crypto.createHmac('md5', key)
    .update(text)
    .digest('hex')
msg.payload = gen_hmac
return msg;

```

```auto
[{"id":"9fcd0d88c89ef781","type":"function","z":"02f9e197120dd932","name":"hmac","func":"const text = 'I love cupcakes'\nconst key = 'abcdeg'\n\n\nconst gen_hmac = crypto.createHmac('md5', key)\n .update(text)\n .digest('hex')\nmsg.payload = gen_hmac\nreturn msg;","outputs":1,"timeout":0,"noerr":1,"initialize":"","finalize":"","libs":[],"x":850,"y":260,"wires":[["456c3c15f6eccc6f"]]},{"id":"c91edeaad15ab4ac","type":"inject","z":"02f9e197120dd932","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":680,"y":260,"wires":[["9fcd0d88c89ef781"]]},{"id":"456c3c15f6eccc6f","type":"debug","z":"02f9e197120dd932","name":"debug 448","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 August 2024 08:27 UTC](https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170/2 "2024-08-14T08:27:17Z")

</div>

> [@kitori](#):
>
> Is it possible to use node:crypto in a function node?

Yes.

> **[Use any npm module in Node-RED • FlowFuse](https://flowfuse.com/blog/2023/06/import-modules/)**
>
> Node-RED has an incredibly rich resource of integrations available, but sometimes you need that little extra. This shows you how.

Just add it to the setup tab.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3587c09c8712b8203536dc3844810a8a17a263e.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f9c49298447835601c18f70f2d9bfd8a79c271c.png)

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [14 August 2024 08:29 UTC](https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170/3 "2024-08-14T08:29:59Z")

</div>

lol, i was just to stupid... need more coffee. Thanks!

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [14 August 2024 08:39 UTC](https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170/4 "2024-08-14T08:39:07Z")

</div>

openssl version, if someone want to use this in an exec node:

```auto
echo -n "I love cupcakes" | openssl dgst -md5 -hmac "abcdeg" "$@" | awk '{print $2}'

```

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/7369d0e20fe4a0875a8e450d8ac6574c37abf432.png)

```auto

[{"id":"f1dd97b244c049de","type":"exec","z":"02f9e197120dd932","command":"echo -n \"I love cupcakes\" | openssl dgst -md5 -hmac \"abcdeg\" \"$@\" | awk '{print $2}'","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"openssl","x":700,"y":360,"wires":[["44703693989380fe"],[],[]]},{"id":"98c7f1cbf9c6c6c9","type":"inject","z":"02f9e197120dd932","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":520,"y":360,"wires":[["f1dd97b244c049de"]]},{"id":"44703693989380fe","type":"debug","z":"02f9e197120dd932","name":"debug 449","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":360,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 August 2024 08:39 UTC](https://discourse.nodered.org/t/how-to-use-node-module-crypto-correctly/90170/5 "2024-08-28T08:39:23Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
