UNPKG

n8n-nodes-aes

Version:

n8n community node to encrypt and decrypt data using crypto-js.

260 lines 9.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Aes = void 0; const CryptoJS = require('crypto-js'); function decrypt(cipherText, encoding, key, mode, pad, iv) { if (cipherText) { if (encoding === 'base64') { cipherText = CryptoJS.enc.Base64.parse(cipherText); } else if (encoding === 'hex') { cipherText = CryptoJS.enc.Hex.parse(cipherText); } const decrypted = CryptoJS.AES.decrypt({ ciphertext: cipherText }, key, buildConfig(mode, pad, iv)); return decrypted.toString(CryptoJS.enc.Utf8); } } function encrypt(plainText, encoding, key, mode, pad, iv) { if (plainText) { const conf = buildConfig(mode, pad, iv); const cipherText = CryptoJS.AES.encrypt(plainText, key, conf); if (encoding === 'base64') { return cipherText.toString(); } else if (encoding === 'hex') { return CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(cipherText)); } } } function buildConfig(mode, pad, iv) { let conf = {}; switch (mode) { case 'cbc': conf.mode = CryptoJS.mode.CBC; break; case 'cfb': conf.mode = CryptoJS.mode.CFB; break; case 'ctr': conf.mode = CryptoJS.mode.CTR; break; case 'ofb': conf.mode = CryptoJS.mode.OFB; break; case 'ecb': conf.mode = CryptoJS.mode.ECB; break; } switch (pad) { case 'pkcs7': conf.padding = CryptoJS.pad.Pkcs7; break; case 'iso97971': conf.padding = CryptoJS.pad.Iso97971; break; case 'ansix923': conf.padding = CryptoJS.pad.AnsiX923; break; case 'iso10126': conf.padding = CryptoJS.pad.Iso10126; break; case 'zeropadding': conf.padding = CryptoJS.pad.ZeroPadding; break; case 'nopadding': conf.padding = CryptoJS.pad.NoPadding; break; } conf.iv = iv; return conf; } class Aes { constructor() { this.description = { displayName: 'AES', name: 'AES', icon: 'file:pgplogo.svg', group: ['input'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Use AES to encrypt or decrypt data', defaults: { name: 'AES', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'aesKey', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Decrypt', value: 'decrypt', }, { name: 'Encrypt', value: 'encrypt', }, ], default: 'decrypt', description: 'Which operation to use?', }, { displayName: 'Mode', name: 'mode', type: 'options', options: [ { name: 'CBC', value: 'cbc', }, { name: 'CFB', value: 'cfb', }, { name: 'CTR', value: 'ctr', }, { name: 'ECB', value: 'ecb', }, { name: 'OFB', value: 'ofb', }, ], default: 'cbc', }, { displayName: 'Padding', name: 'padding', type: 'options', options: [ { name: 'AnsiX923', value: 'ansix923', }, { name: 'Iso10126', value: 'iso10126', }, { name: 'Iso97971', value: 'iso97971', }, { name: 'NoPadding', value: 'nopadding', }, { name: 'Pkcs7', value: 'pkcs7', }, { name: 'ZeroPadding', value: 'zeropadding', }, ], default: 'pkcs7', }, { displayName: 'Encoding', name: 'encoding', type: 'options', options: [ { name: 'Base64', value: 'base64', }, { name: 'Hex', value: 'hex', }, ], default: 'base64', }, { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'String', value: 'string', }, ], default: 'string', }, { displayName: 'Text', name: 'text', type: 'string', default: '', displayOptions: { show: { type: ['string'], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('aesKey'); let responseData; const key = CryptoJS.enc.Utf8.parse(credentials.key.slice(0, 16)); const iv = CryptoJS.enc.Utf8.parse(credentials.key.slice(16, 32)); for (let i = 0; i < items.length; i++) { const dataType = this.getNodeParameter('type', i); const cipherMode = this.getNodeParameter('mode', i); const padding = this.getNodeParameter('padding', i); const encoding = this.getNodeParameter('encoding', i); const text = this.getNodeParameter('text', i); try { if (operation === 'decrypt') { if (dataType === 'string') { const decrypted = decrypt(text, encoding, key, cipherMode, padding, iv); responseData = [{ decrypted }]; } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } if (operation === 'encrypt') { if (dataType === 'string') { const encrypted = encrypt(text, encoding, key, cipherMode, padding, iv); responseData = [{ encrypted }]; } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } } return this.prepareOutputData(returnData); } } exports.Aes = Aes; //# sourceMappingURL=Aes.node.js.map