From 397983c25210df04e79c9fef7395b4bb2654647b Mon Sep 17 00:00:00 2001 From: pemontto Date: Sun, 23 Jan 2022 21:10:35 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20generate=20action=20to=20cryp?= =?UTF-8?q?to=20node?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nodes-base/nodes/Crypto/Crypto.node.ts | 89 ++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Crypto/Crypto.node.ts b/packages/nodes-base/nodes/Crypto/Crypto.node.ts index f8cd3cec7f33d..a9026542caac3 100644 --- a/packages/nodes-base/nodes/Crypto/Crypto.node.ts +++ b/packages/nodes-base/nodes/Crypto/Crypto.node.ts @@ -14,6 +14,8 @@ import { createHmac, createSign, getHashes, + randomBytes, + randomUUID, } from 'crypto'; export class Crypto implements INodeType { @@ -52,6 +54,11 @@ export class Crypto implements INodeType { description: 'Sign a string using a private key.', value: 'sign', }, + { + name: 'Generate', + description: 'Generate random data.', + value: 'generate', + }, ], default: 'hash', }, @@ -332,6 +339,73 @@ export class Crypto implements INodeType { default: '', required: true, }, + { + displayName: 'Property Name', + name: 'dataPropertyName', + type: 'string', + default: 'data', + required: true, + displayOptions: { + show: { + action: [ + 'generate', + ], + }, + }, + description: 'Name of the property to which to write the random string.', + }, + { + displayName: 'Type', + name: 'type', + displayOptions: { + show: { + action: [ + 'generate', + ], + }, + }, + type: 'options', + options: [ + { + name: 'ASCII', + value: 'ascii', + }, + { + name: 'Base64', + value: 'base64', + }, + { + name: 'Hex', + value: 'hex', + }, + { + name: 'UUID', + value: 'uuid', + }, + ], + default: 'uuid', + description: 'The random string type to generate.', + required: true, + }, + { + displayName: 'Length', + name: 'stringLength', + displayOptions: { + show: { + action: [ + 'generate', + ], + type: [ + 'ascii', + 'base64', + 'hex', + ], + }, + }, + type: 'number', + default: 32, + description: 'Length of the random string.', + }, ], }; @@ -369,9 +443,22 @@ export class Crypto implements INodeType { item = items[i]; const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string; - const value = this.getNodeParameter('value', i) as string; + const value = this.getNodeParameter('value', i, '') as string; let newValue; + if (action === 'generate') { + const type = this.getNodeParameter('type', i) as string; + if ( type === 'uuid') { + newValue = randomUUID(); + } else { + const stringLength = this.getNodeParameter('stringLength', i) as number; + if (type === 'base64') { + newValue = randomBytes(stringLength).toString(type as BufferEncoding).replace(/\W/g, '').slice(0, stringLength); + } else { + newValue = randomBytes(stringLength).toString(type as BufferEncoding).slice(0, stringLength); + } + } + } if (action === 'hash') { const type = this.getNodeParameter('type', i) as string; const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;