Improvement on Crypto-Node

This commit is contained in:
Jan Oberhauser 2020-02-07 22:55:58 -08:00
parent 2296a0173f
commit b471c0f8b2

View file

@ -1,3 +1,4 @@
import { set } from 'lodash';
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
INodeExecutionData, INodeExecutionData,
@ -349,48 +350,60 @@ export class Crypto implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = [];
const returnData: INodeExecutionData[] = [];
const length = items.length as unknown as number; const length = items.length as unknown as number;
let responseData;
const action = this.getNodeParameter('action', 0) as string; const action = this.getNodeParameter('action', 0) as string;
let item: INodeExecutionData;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
item = items[i];
const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string; 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 === 'hash') { if (action === 'hash') {
const type = this.getNodeParameter('type', i) as string; const type = this.getNodeParameter('type', i) as string;
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding; const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
const clone = { ...items[i].json }; newValue = createHash(type).update(value).digest(encoding);
clone[dataPropertyName] = createHash(type).update(value).digest(encoding);
responseData = clone;
} }
if (action === 'hmac') { if (action === 'hmac') {
const type = this.getNodeParameter('type', i) as string; const type = this.getNodeParameter('type', i) as string;
const secret = this.getNodeParameter('secret', i) as string; const secret = this.getNodeParameter('secret', i) as string;
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding; const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
const clone = { ...items[i].json }; newValue = createHmac(type, secret).update(value).digest(encoding);
clone[dataPropertyName] = createHmac(type, secret).update(value).digest(encoding);
responseData = clone;
} }
if (action === 'sign') { if (action === 'sign') {
const algorithm = this.getNodeParameter('algorithm', i) as string; const algorithm = this.getNodeParameter('algorithm', i) as string;
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding; const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
const privateKey = this.getNodeParameter('privateKey', i) as string; const privateKey = this.getNodeParameter('privateKey', i) as string;
const clone = { ...items[i].json };
const sign = createSign(algorithm); const sign = createSign(algorithm);
sign.write(value as string); sign.write(value as string);
sign.end(); sign.end();
const signature = sign.sign(privateKey, encoding); newValue = sign.sign(privateKey, encoding);
clone[dataPropertyName] = signature;
responseData = clone;
} }
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]); let newItem: INodeExecutionData;
if (dataPropertyName.includes('.')) {
// Uses dot notation so copy all data
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
};
} else { } else {
returnData.push(responseData as IDataObject); // Does not use dot notation so shallow copy is enough
} newItem = {
} json: { ...item.json },
return [this.helpers.returnJsonArray(returnData)]; };
}
if (item.binary !== undefined) {
newItem.binary = item.binary;
}
set(newItem, `json.${dataPropertyName}`, newValue);
returnData.push(newItem);
}
return this.prepareOutputData(returnData);
} }
} }