Light weight cipher for payload protection

I concur however @sreelakshmi25996 was keen to have it encrypted - I assume the problem he is solving for is a shared mqtt broker where you don’t trust the clients and you want to encrypt certain payloads

Anyway a useful experience for me, have used @Steve-Mcl awesome node to measure a flow run time :sunglasses:

Actually, since this is likely all done in the node loop, the delays might not be a true reflection of time taken due to the potential blocking caused by the heavy encryption/decryption processing.

Create 2 separate flows and test again. (Operate them separately)

Results might show a different story.

fair point

So bottom line the delta of ~ 2 secs remains the same but percentage wise it doubles

[{"id":"9a693308.7ebaf","type":"subflow","name":"flow-timer","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"7fc82258.93e36c"}]}],"out":[{"x":440,"y":100,"wires":[{"id":"7fc82258.93e36c","port":0}]}],"env":[{"name":"name","type":"str","value":"measure","ui":{"icon":"font-awesome/fa-tag","label":{"en-US":"Timer Name"},"type":"input","opts":{"types":["str","env"]}}},{"name":"operation","type":"str","value":"start","ui":{"icon":"font-awesome/fa-cog","label":{"en-US":"Operation"},"type":"select","opts":{"opts":[{"l":{"en-US":"start"},"v":"start"},{"l":{"en-US":"stop"},"v":"stop"},{"l":{"en-US":"msg.topic"},"v":"msg.topic"},{"l":{"en-US":"msg.operation"},"v":"msg.operation"},{"l":{"en-US":"msg.payload"},"v":"msg.payload"}]}}}],"color":"#DAEAAA","icon":"node-red/timer.svg","status":{"x":280,"y":160,"wires":[{"id":"7fc82258.93e36c","port":1}]}},{"id":"7fc82258.93e36c","type":"function","z":"9a693308.7ebaf","name":"do operation","func":"// @ts-ignore\nvar name = msg.perfName || env.get(\"name\");\n// @ts-ignore\nvar operation = msg.perfOperation || env.get(\"operation\");\nvar measures = global.get(\"flow_timers\") || {};\nvar measure = measures[name] || {};\n\nfunction doOp(measure, op){\n    if(operation === \"start\"){\n        measure.start = Date.now();//change to process.hrtime\n        measure.stop = null;\n        measure.durationMs = null;\n    } else if(operation === \"stop\") {\n        measure.stop = Date.now();//change to process.hrtime\n        measure.durationMs = measure.start ? measure.stop - measure.start : null;\n        msg._performance = measure;\n    }\n}\n\n\nif(operation === \"start\"){\n    doOp(measure, operation);\n} else if(operation === \"stop\") {\n    doOp(measure, operation);\n    node.send([null, { payload: { text: name + \": \" + measure.durationMs + \"ms\" }}]);\n} else if(operation === \"msg.topic\") {\n    operation = msg.topic;\n    doOp(measure, operation);\n} else if(operation === \"msg.operation\") {\n    operation = msg.operation;\n    doOp(measure, operation);\n} else if(operation === \"msg.payload\") {\n    operation = msg.payload;\n    doOp(measure, operation);\n} else {\n    return [msg, null];\n}\nmeasures[name] = measure;\nglobal.set(\"flow_timers\", measures);\n\nreturn [msg, null];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":250,"y":100,"wires":[[],[]]},{"id":"3bfb9bc3.49da3c","type":"switch","z":"bce50b9f.7a7508","name":"Which point","property":"payload.payload","propertyType":"msg","rules":[{"t":"hask","v":"RADamperPos","vt":"str"},{"t":"hask","v":"SetUnitEnabled","vt":"str"},{"t":"hask","v":"HtgSP","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":250,"y":345,"wires":[["2d4f016d.bca636"],["6f3e2ac5.7c17cc"],["81bc7de3.81054"]],"outputLabels":["RADamperPos","SetUnitEnabled","HtgSP"]},{"id":"2d4f016d.bca636","type":"change","z":"bce50b9f.7a7508","name":"RADamperPos","rules":[{"t":"set","p":"payload","pt":"msg","to":"{     \"RADamperPos\" : msg.payload.payload.RADamperPos}        ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":300,"wires":[["258292c7.9d602e"]]},{"id":"81bc7de3.81054","type":"change","z":"bce50b9f.7a7508","name":"HtgSP","rules":[{"t":"set","p":"payload","pt":"msg","to":"{     \"HtgSP\" : msg.payload.payload.HtgSP}        ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":390,"wires":[["258292c7.9d602e"]]},{"id":"6f3e2ac5.7c17cc","type":"change","z":"bce50b9f.7a7508","name":"SetUnitEnabled","rules":[{"t":"set","p":"payload","pt":"msg","to":"{     \"SetUnitEnabled\" : msg.payload.payload.SetUnitEnabled}        ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":345,"wires":[["258292c7.9d602e"]]},{"id":"258292c7.9d602e","type":"debug","z":"bce50b9f.7a7508","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":655,"y":360,"wires":[]},{"id":"11ea05c4.9a26b2","type":"inject","z":"bce50b9f.7a7508","name":"Inject value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"requestId\":\"1\",\"methodName\":\"AHU01_Commands\",\"payload\":{\"RADamperPos\":57.5},\"topic\":\"command\",\"deviceId\":\"MyNodeDevice\"}","payloadType":"json","x":90,"y":345,"wires":[["3bfb9bc3.49da3c"]]},{"id":"ba262e2.e2707d","type":"inject","z":"bce50b9f.7a7508","name":"Inject value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"requestId\":\"1\",\"methodName\":\"AHU01_Commands\",\"payload\":{\"RADamperPos\":57.5},\"topic\":\"command\",\"deviceId\":\"MyNodeDevice\"}","payloadType":"json","x":120,"y":570,"wires":[["2932ce05.dc5bf2"]]},{"id":"2932ce05.dc5bf2","type":"change","z":"bce50b9f.7a7508","name":"Extract just payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":315,"y":570,"wires":[["68449f71.a0708"]]},{"id":"68449f71.a0708","type":"debug","z":"bce50b9f.7a7508","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":570,"wires":[]},{"id":"472e382.d297ec8","type":"jwt sign","z":"bce50b9f.7a7508","name":"Encrypt","alg":"HS256","exp":3600,"jwkurl":"","jwkkid":"","secret":"node-red","key":"","signvar":"payload","storetoken":"payload","x":540,"y":1350,"wires":[["605ab49e.11aec4"]]},{"id":"1aefae8e.c84941","type":"inject","z":"bce50b9f.7a7508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":195,"y":1095,"wires":[["606742cc.34db2c"]]},{"id":"605ab49e.11aec4","type":"mqtt out","z":"bce50b9f.7a7508","name":"","topic":"/jwt-test-token","qos":"","retain":"","broker":"8d864a45.0e5a9","x":680,"y":1350,"wires":[]},{"id":"d42cbfda.bab528","type":"comment","z":"bce50b9f.7a7508","name":"secret node-red","info":"","x":365,"y":1275,"wires":[]},{"id":"80b9b0b2.1dd84","type":"subflow:9a693308.7ebaf","z":"bce50b9f.7a7508","name":"start jwt","env":[{"name":"name","value":"jwt","type":"str"}],"x":405,"y":1350,"wires":[["472e382.d297ec8"]]},{"id":"f6181e74.1732a","type":"subflow:9a693308.7ebaf","z":"bce50b9f.7a7508","name":"stop jwt","env":[{"name":"name","value":"jwt","type":"str"},{"name":"operation","value":"stop","type":"str"}],"x":1140,"y":1350,"wires":[["189d3e26.ffd122"]]},{"id":"6cb64e4a.f6365","type":"subflow:9a693308.7ebaf","z":"bce50b9f.7a7508","name":"start clear","env":[{"name":"name","value":"clear","type":"str"}],"x":405,"y":1425,"wires":[["66b0446.0b4d63c"]]},{"id":"617e024.a7ac9fc","type":"subflow:9a693308.7ebaf","z":"bce50b9f.7a7508","name":"stop clear","env":[{"name":"name","value":"clear","type":"str"},{"name":"operation","value":"stop","type":"str"}],"x":1140,"y":1425,"wires":[["189d3e26.ffd122"]]},{"id":"189d3e26.ffd122","type":"debug","z":"bce50b9f.7a7508","name":"Log it","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1280,"y":1380,"wires":[]},{"id":"2f4e3a15.2fab26","type":"jwt verify","z":"bce50b9f.7a7508","name":"","alg":["HS256"],"jwkurl":"","secret":"node-red","key":"","signvar":"payload","storetoken":"payload","x":1000,"y":1350,"wires":[["f6181e74.1732a"]]},{"id":"a62c2bfa.fdde4","type":"mqtt in","z":"bce50b9f.7a7508","name":"","topic":"/jwt-test-token","qos":"2","datatype":"auto","broker":"8d864a45.0e5a9","x":845,"y":1350,"wires":[["2f4e3a15.2fab26"]]},{"id":"66b0446.0b4d63c","type":"mqtt out","z":"bce50b9f.7a7508","name":"","topic":"/jwt-test","qos":"","retain":"","broker":"8d864a45.0e5a9","x":555,"y":1425,"wires":[]},{"id":"3f0baaf3.68cafe","type":"mqtt in","z":"bce50b9f.7a7508","name":"","topic":"/jwt-test","qos":"2","datatype":"auto","broker":"8d864a45.0e5a9","x":825,"y":1425,"wires":[["617e024.a7ac9fc"]]},{"id":"606742cc.34db2c","type":"file in","z":"bce50b9f.7a7508","name":"","filename":"/backups/ha-2021-04-07.tar.gz","format":"","chunk":false,"sendError":false,"encoding":"none","x":415,"y":1095,"wires":[["b95bf87.e7add88"]]},{"id":"b95bf87.e7add88","type":"function","z":"bce50b9f.7a7508","name":"","func":"let tmp = {}\n\ntmp.iat = Math.trunc(Date.now()/1000)\ntmp.expiresIn = Math.trunc(Date.now()/20000)\ntmp.token = msg.payload\n\nmsg.payload = tmp\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":195,"y":1350,"wires":[["80b9b0b2.1dd84"]]},{"id":"a1dd6de1.c1d79","type":"debug","z":"bce50b9f.7a7508","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":775,"y":825,"wires":[]},{"id":"d444d24b.a096f","type":"change","z":"bce50b9f.7a7508","name":"RADamperPos","rules":[{"t":"set","p":"payload","pt":"msg","to":"{     \"RADamperPos\" : msg.payload.payload.RADamperPos}        ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":765,"wires":[["a1dd6de1.c1d79"]]},{"id":"21c33452.ae55ac","type":"change","z":"bce50b9f.7a7508","name":"SetUnitEnabled","rules":[{"t":"set","p":"payload","pt":"msg","to":"{     \"SetUnitEnabled\" : msg.payload.payload.SetUnitEnabled}        ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":810,"wires":[["a1dd6de1.c1d79"]]},{"id":"a780e14a.a0e168","type":"change","z":"bce50b9f.7a7508","name":"HtgSP","rules":[{"t":"set","p":"payload","pt":"msg","to":"{     \"HtgSP\" : msg.payload.payload.HtgSP}        ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":855,"wires":[["a1dd6de1.c1d79"]]},{"id":"d74ca36c.e128","type":"switch","z":"bce50b9f.7a7508","name":"Which point","property":"payload.payload","propertyType":"msg","rules":[{"t":"hask","v":"RADamperPos","vt":"str"},{"t":"hask","v":"SetUnitEnabled","vt":"str"},{"t":"hask","v":"HtgSP","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":370,"y":810,"wires":[["d444d24b.a096f"],["21c33452.ae55ac"],["a780e14a.a0e168"]],"outputLabels":["RADamperPos","SetUnitEnabled","HtgSP"]},{"id":"e093da07.64977","type":"inject","z":"bce50b9f.7a7508","name":"Inject value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"requestId\":\"1\",\"methodName\":\"AHU01_Commands\",\"payload\":{\"RADamperPos\":57.5},\"topic\":\"command\",\"deviceId\":\"MyNodeDevice\"}","payloadType":"json","x":210,"y":810,"wires":[["d74ca36c.e128"]]},{"id":"7b943028.8f8ff","type":"trigger","z":"bce50b9f.7a7508","name":"","op1":"1","op2":"0","op1type":"val","op2type":"val","duration":"250","extend":"false","overrideDelay":"false","units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":390,"y":165,"wires":[[]]},{"id":"2781bae1.b1bae6","type":"function","z":"bce50b9f.7a7508","name":"","func":"let tmp = {}\n\ntmp.iat = Math.trunc(Date.now()/1000)\ntmp.expiresIn = Math.trunc(Date.now()/20000)\ntmp.token = msg.payload\n\nmsg.payload = tmp\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":210,"y":1425,"wires":[["6cb64e4a.f6365"]]},{"id":"26aab639.4ace72","type":"file in","z":"bce50b9f.7a7508","name":"","filename":"/backups/ha-2021-04-07.tar.gz","format":"","chunk":false,"sendError":false,"encoding":"none","x":250,"y":1590,"wires":[["2781bae1.b1bae6"]]},{"id":"d21a770c.eb5688","type":"inject","z":"bce50b9f.7a7508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":165,"y":1680,"wires":[["26aab639.4ace72"]]},{"id":"8d864a45.0e5a9","type":"mqtt-broker","name":"","broker":"mqtt","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]
1 Like

Good stuff. For a smallish image (5mb) this would equate to sub 1sec. So as long as the op is not wanting to do 25 frames per second, a fairly decent result.

btw @Steve-Mcl thanks for your timer sub flow. Have just learnt a whole heap about subflows and env variables.

This is very cool, especially in 1.3 when they can be exported to modules . Thanks

2 Likes

That can be coped with by the technique I mentioned earlier, which is to allow only particular MQTT users to access the sensitive topics.

1 Like