Skip to content

Commit

Permalink
✨ Add generate action to crypto node
Browse files Browse the repository at this point in the history
  • Loading branch information
pemontto committed Dec 9, 2021
1 parent 6453996 commit feae105
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions packages/nodes-base/nodes/Crypto/Crypto.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
createHmac,
createSign,
getHashes,
randomBytes,
randomUUID,
} from 'crypto';

export class Crypto implements INodeType {
Expand Down Expand Up @@ -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',
},
Expand Down Expand Up @@ -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.',
},
],
};

Expand Down Expand Up @@ -369,6 +443,24 @@ export class Crypto implements INodeType {

item = items[i];
const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string;

if (action === 'generate') {
const type = this.getNodeParameter('type', i) as string;
const newItem = { json: {}} as INodeExecutionData;
if ( type === 'uuid') {
newItem.json[dataPropertyName] = randomUUID();
} else {
const stringLength = this.getNodeParameter('stringLength', i) as number;
if (type === 'base64') {
newItem.json[dataPropertyName] = randomBytes(stringLength).toString(type as BufferEncoding).replace(/\W/g, '').slice(0, stringLength);
} else {
newItem.json[dataPropertyName] = randomBytes(stringLength).toString(type as BufferEncoding).slice(0, stringLength);
}
}
returnData.push(newItem);
continue;
}

const value = this.getNodeParameter('value', i) as string;
let newValue;

Expand Down

0 comments on commit feae105

Please sign in to comment.