mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 11:01:15 +00:00
feat(Crypto Node): Add support for hash and hmac on binary data (#6359)
This commit is contained in:
committed by
GitHub
parent
9dfc11037b
commit
406a405dd1
@@ -9,13 +9,17 @@ import type {
|
||||
INodeTypeDescription,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
import { deepCopy } from 'n8n-workflow';
|
||||
import { deepCopy, BINARY_ENCODING } from 'n8n-workflow';
|
||||
|
||||
import type { BinaryToTextEncoding } from 'crypto';
|
||||
import { createHash, createHmac, createSign, getHashes, randomBytes } from 'crypto';
|
||||
import stream from 'stream';
|
||||
import { promisify } from 'util';
|
||||
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
const pipeline = promisify(stream.pipeline);
|
||||
|
||||
export class Crypto implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Crypto',
|
||||
@@ -45,15 +49,15 @@ export class Crypto implements INodeType {
|
||||
},
|
||||
{
|
||||
name: 'Hash',
|
||||
description: 'Hash a text in a specified format',
|
||||
description: 'Hash a text or file in a specified format',
|
||||
value: 'hash',
|
||||
action: 'Hash a text in a specified format',
|
||||
action: 'Hash a text or file in a specified format',
|
||||
},
|
||||
{
|
||||
name: 'Hmac',
|
||||
description: 'Hmac a text in a specified format',
|
||||
description: 'Hmac a text or file in a specified format',
|
||||
value: 'hmac',
|
||||
action: 'HMAC a text in a specified format',
|
||||
action: 'HMAC a text or file in a specified format',
|
||||
},
|
||||
{
|
||||
name: 'Sign',
|
||||
@@ -107,12 +111,40 @@ export class Crypto implements INodeType {
|
||||
description: 'The hash type to use',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Binary Data',
|
||||
name: 'binaryData',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
action: ['hash', 'hmac'],
|
||||
},
|
||||
},
|
||||
description: 'Whether the data to hashed should be taken from binary field',
|
||||
},
|
||||
{
|
||||
displayName: 'Binary Property Name',
|
||||
name: 'binaryPropertyName',
|
||||
displayOptions: {
|
||||
show: {
|
||||
action: ['hash', 'hmac'],
|
||||
binaryData: [true],
|
||||
},
|
||||
},
|
||||
type: 'string',
|
||||
default: 'data',
|
||||
description: 'Name of the binary property which contains the input data',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'value',
|
||||
displayOptions: {
|
||||
show: {
|
||||
action: ['hash'],
|
||||
binaryData: [false],
|
||||
},
|
||||
},
|
||||
type: 'string',
|
||||
@@ -204,6 +236,7 @@ export class Crypto implements INodeType {
|
||||
displayOptions: {
|
||||
show: {
|
||||
action: ['hmac'],
|
||||
binaryData: [false],
|
||||
},
|
||||
},
|
||||
type: 'string',
|
||||
@@ -264,6 +297,7 @@ export class Crypto implements INodeType {
|
||||
displayOptions: {
|
||||
show: {
|
||||
action: ['sign'],
|
||||
binaryData: [false],
|
||||
},
|
||||
},
|
||||
type: 'string',
|
||||
@@ -430,6 +464,7 @@ export class Crypto implements INodeType {
|
||||
const dataPropertyName = this.getNodeParameter('dataPropertyName', i);
|
||||
const value = this.getNodeParameter('value', i, '') as string;
|
||||
let newValue;
|
||||
let binaryProcessed = false;
|
||||
|
||||
if (action === 'generate') {
|
||||
const encodingType = this.getNodeParameter('encodingType', i);
|
||||
@@ -449,17 +484,33 @@ export class Crypto implements INodeType {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (action === 'hash') {
|
||||
|
||||
if (action === 'hash' || action === 'hmac') {
|
||||
const type = this.getNodeParameter('type', i) as string;
|
||||
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
|
||||
newValue = createHash(type).update(value).digest(encoding);
|
||||
}
|
||||
if (action === 'hmac') {
|
||||
const type = this.getNodeParameter('type', i) as string;
|
||||
const secret = this.getNodeParameter('secret', i) as string;
|
||||
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
|
||||
newValue = createHmac(type, secret).update(value).digest(encoding);
|
||||
const hashOrHmac =
|
||||
action === 'hash'
|
||||
? createHash(type)
|
||||
: createHmac(type, this.getNodeParameter('secret', i) as string);
|
||||
if (this.getNodeParameter('binaryData', i)) {
|
||||
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
||||
if (binaryData.id) {
|
||||
const binaryStream = this.helpers.getBinaryStream(binaryData.id);
|
||||
hashOrHmac.setEncoding(encoding);
|
||||
await pipeline(binaryStream, hashOrHmac);
|
||||
newValue = hashOrHmac.read();
|
||||
} else {
|
||||
newValue = hashOrHmac
|
||||
.update(Buffer.from(binaryData.data, BINARY_ENCODING))
|
||||
.digest(encoding);
|
||||
}
|
||||
binaryProcessed = true;
|
||||
} else {
|
||||
newValue = hashOrHmac.update(value).digest(encoding);
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'sign') {
|
||||
const algorithm = this.getNodeParameter('algorithm', i) as string;
|
||||
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
|
||||
@@ -489,7 +540,7 @@ export class Crypto implements INodeType {
|
||||
};
|
||||
}
|
||||
|
||||
if (item.binary !== undefined) {
|
||||
if (item.binary !== undefined && !binaryProcessed) {
|
||||
newItem.binary = item.binary;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user