n8n-nodes-sm2-crypto
Version:
SM2 encryption and decryption node for n8n
93 lines • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sm2Crypto = void 0;
const sm_crypto_1 = require("sm-crypto");
class Sm2Crypto {
constructor() {
this.description = {
displayName: 'SM2 Crypto',
name: 'sm2Crypto',
group: ['transform'],
version: 1,
description: 'Encrypt or decrypt strings using the SM2 algorithm',
defaults: {
name: 'SM2 Crypto',
},
icon: 'file:./sm2.logo.svg',
inputs: ["main"],
outputs: ["main"],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{ name: 'Encrypt', value: 'encrypt' },
{ name: 'Decrypt', value: 'decrypt' },
],
default: 'encrypt',
noDataExpression: true,
},
{
displayName: 'Token (Key)',
name: 'token',
type: 'string',
default: '',
required: true,
typeOptions: {
password: true,
},
description: 'Public key for encryption, or private key for decryption',
},
{
displayName: 'Input Field Name',
name: 'inputField',
type: 'string',
default: 'data',
description: 'Name of the field to encrypt or decrypt',
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
const operation = this.getNodeParameter('operation', i);
const token = this.getNodeParameter('token', i);
const inputField = this.getNodeParameter('inputField', i);
const value = items[i].json[inputField];
let result;
try {
if (operation === 'encrypt') {
const plaintext = typeof value === 'string' ? value : JSON.stringify(value);
const cipher = sm_crypto_1.sm2.doEncrypt(plaintext, token, 1);
result = { ciphertext: '04' + cipher };
}
else {
let cipher = typeof value === 'string' ? value : String(value);
if (cipher.startsWith('04'))
cipher = cipher.slice(2);
let decrypted = sm_crypto_1.sm2.doDecrypt(cipher, token, 1);
try {
result = JSON.parse(decrypted);
}
catch (error) {
result = { error, plaintext: decrypted };
}
}
}
catch (error) {
result = { error: error.message };
}
returnData.push({
json: {
...result,
},
});
}
return [returnData];
}
}
exports.Sm2Crypto = Sm2Crypto;
//# sourceMappingURL=Sm2Crypto.node.js.map