# A Simple CRC Checksum Node (Subflow)

**URL:** <https://discourse.nodered.org/t/a-simple-crc-checksum-node-subflow/82429>\
**Category:** Share Your Nodes\
**Tags:** crc, checksum\
**Created:** [28 October 2023 18:05 UTC](https://discourse.nodered.org/t/a-simple-crc-checksum-node-subflow/82429 "2023-10-28T18:05:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [28 October 2023 18:05 UTC](https://discourse.nodered.org/t/a-simple-crc-checksum-node-subflow/82429/1 "2023-10-28T18:05:08Z")

</div>

This isn't a published node, but a subflow to easily validate checksums.

The subflow requires the following inputs:

- `crcLength` : example : 8
- `crcChecksum` : example : "362d"
- `crcAlgorithm` : example: "XMODEM"

The check will be applied against the `payload` - so you will need to remove any checksum that maybe part of the value to be checked.

The subflow will check the type of the `payload`  
and if its a:

- Buffer - will use as is
- String - will create a buffer from it
- Object - will stringify it before creating a buffer from it

The subflow will output the following properties

- `crcResult` (boolean)
- `calculatedCrc` (the calculated Checksum)

Anyway, its no fuss, but was inspired by some code @Steve-Mcl provided in a recent thread.  
You _may_ need to enable the import of modules in function nodes to use it.

Quality control is minimal here - it was a quick and dirty thing I put together for fun, but should work.

Enjoy

 ![Screenshot 2023-10-28 at 18.59.43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2d213a278f882635e318d1300895d3efff27d63.png)

```auto
[{"id":"876f9b353ddfb631","type":"subflow","name":"CRC Check","info":"","category":"","in":[{"x":195,"y":105,"wires":[{"id":"7b3bc1491b1d77b1"}]}],"out":[{"x":495,"y":70,"wires":[{"id":"7b3bc1491b1d77b1","port":0}]}],"env":[],"meta":{},"color":"#3FADB5","icon":"font-awesome/fa-check","status":{"x":495,"y":130,"wires":[{"id":"7b3bc1491b1d77b1","port":1}]}},{"id":"7b3bc1491b1d77b1","type":"function","z":"876f9b353ddfb631","name":"function 25","func":"const CRC = msg.crcChecksum\nconst CRCLength = msg.crcLength\nconst CRCAlgorithm = msg.crcAlgorithm;\n\nlet ValueToCheck;\n\nif(!Buffer.isBuffer(msg.payload)){\n \n if(typeof msg.payload === 'object'){\n ValueToCheck = Buffer.from(JSON.stringify(msg.payload),'utf-8')\n }\n \n if(typeof msg.payload === 'string'){\n ValueToCheck = Buffer.from(msg.payload,'utf-8')\n }\n}\nelse{\n ValueToCheck = msg.payload\n}\n\nconst Result = easyCrc[`crc${CRCLength}`](CRCAlgorithm,ValueToCheck);\n\nmsg.crcResult = Result.toString(16) === CRC\nmsg.calculatedCrc = Result.toString(16)\n\nconst Status = {\n fill:msg.crcResult ? \"green\" : \"red\",\n shape:\"dot\",\n text:msg.crcResult\n \n}\n\n\nreturn [msg,{payload:Status}]\n\n","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"easyCrc","module":"easy-crc"}],"x":335,"y":105,"wires":[[],[]]},{"id":"68574cd59b9e4bfd","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"04add5a83a77c4e0","type":"subflow:876f9b353ddfb631","z":"68574cd59b9e4bfd","name":"","x":460,"y":285,"wires":[["0e2a11d70ae5c51e"]]},{"id":"7b842a07194a5bdb","type":"inject","z":"68574cd59b9e4bfd","name":"Not Modifed","props":[{"p":"payload"},{"p":"crcLength","v":"16","vt":"str"},{"p":"crcAlgorithm","v":"XMODEM","vt":"str"},{"p":"crcChecksum","v":"362d","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Hello, World","payloadType":"str","x":255,"y":200,"wires":[["04add5a83a77c4e0"]]},{"id":"0e2a11d70ae5c51e","type":"debug","z":"68574cd59b9e4bfd","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":665,"y":285,"wires":[]},{"id":"05715addafd2f3d7","type":"inject","z":"68574cd59b9e4bfd","name":"Modifed","props":[{"p":"payload"},{"p":"crcLength","v":"16","vt":"str"},{"p":"crcAlgorithm","v":"XMODEM","vt":"str"},{"p":"crcChecksum","v":"362d","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Hello,World","payloadType":"str","x":240,"y":315,"wires":[["04add5a83a77c4e0"]]}]

```
