Skip to content

Commit

Permalink
feat(Crypto Node): Add Generate operation to generate random values (#…
Browse files Browse the repository at this point in the history
…2541)

* ✨ Add generate action to crypto node

* ⚡ small fixes, nodelinter issues fixes

* ⚡ Improvements

* ⚡ Fix order

Co-authored-by: michael-radency <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
  • Loading branch information
4 people authored Mar 27, 2022
1 parent ab08c0d commit b5ecccb
Showing 1 changed file with 110 additions and 15 deletions.
125 changes: 110 additions & 15 deletions packages/nodes-base/nodes/Crypto/Crypto.node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { set } from 'lodash';
import { IExecuteFunctions } from 'n8n-core';
import {
set,
} from 'lodash';

import {
IExecuteFunctions,
} from 'n8n-core';

import {
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
} from 'n8n-workflow';

import {
Expand All @@ -14,8 +21,11 @@ import {
createHmac,
createSign,
getHashes,
randomBytes,
} from 'crypto';

import { v4 as uuid } from 'uuid';

export class Crypto implements INodeType {
description: INodeTypeDescription = {
displayName: 'Crypto',
Expand All @@ -37,19 +47,24 @@ export class Crypto implements INodeType {
name: 'action',
type: 'options',
options: [
{
name: 'Generate',
description: 'Generate random string',
value: 'generate',
},
{
name: 'Hash',
description: 'Hash a text in a specified format.',
description: 'Hash a text in a specified format',
value: 'hash',
},
{
name: 'Hmac',
description: 'Hmac a text in a specified format.',
description: 'Hmac a text in a specified format',
value: 'hmac',
},
{
name: 'Sign',
description: 'Sign a string using a private key.',
description: 'Sign a string using a private key',
value: 'sign',
},
],
Expand Down Expand Up @@ -100,7 +115,7 @@ export class Crypto implements INodeType {
},
type: 'string',
default: '',
description: 'The value that should be hashed.',
description: 'The value that should be hashed',
required: true,
},
{
Expand All @@ -116,7 +131,7 @@ export class Crypto implements INodeType {
],
},
},
description: 'Name of the property to which to write the hash.',
description: 'Name of the property to which to write the hash',
},
{
displayName: 'Encoding',
Expand Down Expand Up @@ -187,7 +202,7 @@ export class Crypto implements INodeType {
},
type: 'string',
default: '',
description: 'The value of which the hmac should be created.',
description: 'The value of which the hmac should be created',
required: true,
},
{
Expand All @@ -203,7 +218,7 @@ export class Crypto implements INodeType {
],
},
},
description: 'Name of the property to which to write the hmac.',
description: 'Name of the property to which to write the hmac',
},
{
displayName: 'Secret',
Expand Down Expand Up @@ -255,7 +270,7 @@ export class Crypto implements INodeType {
},
type: 'string',
default: '',
description: 'The value that should be signed.',
description: 'The value that should be signed',
required: true,
},
{
Expand All @@ -271,7 +286,7 @@ export class Crypto implements INodeType {
],
},
},
description: 'Name of the property to which to write the signed value.',
description: 'Name of the property to which to write the signed value',
},
{
displayName: 'Algorithm',
Expand Down Expand Up @@ -328,10 +343,77 @@ export class Crypto implements INodeType {
typeOptions: {
alwaysOpenEditWindow: true,
},
description: 'Private key to use when signing the string.',
description: 'Private key to use when signing the string',
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: 'encodingType',
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: 'Encoding that will be used to generate string',
required: true,
},
{
displayName: 'Length',
name: 'stringLength',
type: 'number',
default: 32,
description: 'Length of the generated string',
displayOptions: {
show: {
action: [
'generate',
],
encodingType: [
'ascii',
'base64',
'hex',
],
},
},
},
],
};

Expand Down Expand Up @@ -369,9 +451,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 encodingType = this.getNodeParameter('encodingType', i) as string;
if (encodingType === 'uuid') {
newValue = uuid();
} else {
const stringLength = this.getNodeParameter('stringLength', i) as number;
if (encodingType === 'base64') {
newValue = randomBytes(stringLength).toString(encodingType as BufferEncoding).replace(/\W/g, '').slice(0, stringLength);
} else {
newValue = randomBytes(stringLength).toString(encodingType as BufferEncoding).slice(0, stringLength);
}
}
}
if (action === 'hash') {
const type = this.getNodeParameter('type', i) as string;
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
Expand Down Expand Up @@ -413,10 +508,10 @@ export class Crypto implements INodeType {
set(newItem, `json.${dataPropertyName}`, newValue);

returnData.push(newItem);

} catch (error) {
if (this.continueOnFail()) {
returnData.push({json:{ error: error.message }});
returnData.push({ json: { error: (error as JsonObject).message } });
continue;
}
throw error;
Expand Down

0 comments on commit b5ecccb

Please sign in to comment.