UNPKG

smithtek-nodered-lora-aes

Version:

AES security for the PassPort LoRa Module

63 lines (55 loc) 2.67 kB
module.exports = function(RED) { // Function to calculate CRC function crc(frame) { let sum = 0; for (let i = 0; i < frame.length; i++) { sum += frame[i]; } return sum % 256; } function SmithTekLoRaAESNode(config) { RED.nodes.createNode(this, config); const node = this; // Handle incoming messages node.on('input', function(msg) { // Function to process the message function processMessage(msg) { if (msg.payload === 'enable') { // Fixed enable hex command const enableCommand = Buffer.from([0xAF, 0xAF, 0x00, 0x00, 0xAF, 0x80, 0x40, 0x01, 0x00, 0xCE, 0x0D, 0x0A]); return { payload: enableCommand }; // Send enable command } else if (msg.payload === 'disable') { // Fixed disable hex command const disableCommand = Buffer.from([0xAF, 0xAF, 0x00, 0x00, 0xAF, 0x80, 0x40, 0x01, 0x01, 0xCF, 0x0D, 0x0A]); return { payload: disableCommand }; // Send disable command } else if (typeof msg.key === 'string') { const magicKey = msg.key.trim(); if (magicKey.length === 8) { // Generate magic key hex command const magicKeyCommand = Buffer.from([0xAF, 0xAF, 0x00, 0x00, 0xAF, 0x80, 0x42, 0x08]); const magicKeyAscii = magicKey.split('').map(c => c.charCodeAt(0)); const magicKeyCrc = crc([...magicKeyCommand, ...magicKeyAscii]); const magicKeyHexCommand = Buffer.from([...magicKeyCommand, ...magicKeyAscii, magicKeyCrc, 0x0D, 0x0A]); return { payload: magicKeyHexCommand }; // Send magic key command } else if (magicKey.length < 8) { node.error("Error: Key is too short"); return null; } else { node.error("Error: Key is too long"); return null; } } else { // Invalid payload, do nothing return null; } } // Process the input message const result = processMessage(msg); // Send the result if (result) { node.send(result); } }); } RED.nodes.registerType("smithtek-nodered-lora-aes", SmithTekLoRaAESNode); };