n8n-nodes-wechat
Version:
This is an n8n-wechat-nodes Library
73 lines • 3.43 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = __importDefault(require("crypto"));
class default_1 {
static pkcs7Decode(text) {
if (text.length === 0)
throw new Error('Input buffer is empty');
const pad = text[text.length - 1];
if (pad < 1 || pad > 32 || pad > text.length)
throw new Error('Invalid PKCS#7 padding');
return text.subarray(0, text.length - pad);
}
static pkcs7Encode(text) {
const BLOCK_SIZE = 32;
if (!Buffer.isBuffer(text))
throw new TypeError('Input must be a Buffer');
const textLength = text.length;
const amountToPad = BLOCK_SIZE - (textLength % BLOCK_SIZE);
const result = Buffer.alloc(amountToPad).fill(amountToPad);
return Buffer.concat([text, result]);
}
static checkSignature(signature, token, timestamp, nonce, encrypt = '') {
const hash = crypto_1.default
.createHash('sha1')
.update([token, timestamp, nonce, encrypt].sort().join(''))
.digest('hex');
return hash === signature;
}
static signature(token, timestamp, nonce, encrypt = '') {
return crypto_1.default
.createHash('sha1')
.update([token, timestamp, nonce, encrypt].sort().join(''))
.digest('hex');
}
static decrypt(text, encodingAESKey) {
let key = Buffer.from(encodingAESKey + '=', 'base64');
if (key.length !== 32)
throw new Error('encodingAESKey invalid');
const iv = key.subarray(0, 16);
const decipher = crypto_1.default.createDecipheriv('aes-256-cbc', key, iv).setAutoPadding(false);
let deciphered = Buffer.concat([decipher.update(text, 'base64'), decipher.final()]);
const content = this.pkcs7Decode(deciphered).subarray(16);
const length = content.subarray(0, 4).readUInt32BE(0);
return content.subarray(4, length + 4).toString();
}
static encrypt(text, encodingAESKey, appId = '') {
let key = Buffer.from(encodingAESKey + '=', 'base64');
if (key.length !== 32)
throw new Error('encodingAESKey invalid');
const iv = key.subarray(0, 16);
const randomString = crypto_1.default.pseudoRandomBytes(16);
const msg = Buffer.from(text);
const msgHeader = Buffer.alloc(4);
msgHeader.writeUInt32BE(msg.length, 0);
const id = Buffer.from(appId);
const bufMsg = Buffer.concat([randomString, msgHeader, msg, id]);
const encoded = this.pkcs7Encode(bufMsg);
const cipher = crypto_1.default.createCipheriv('aes-256-cbc', key, iv).setAutoPadding(false);
return Buffer.concat([cipher.update(encoded), cipher.final()]).toString('base64');
}
static encryptResponse(text, encodingAESKey, token, appId) {
const Encrypt = this.encrypt(text, encodingAESKey, appId);
const TimeStamp = parseInt(String(Date.now() / 1000));
const Nonce = Math.random().toString().slice(2, 10);
const MsgSignature = this.signature(token, TimeStamp.toString(), Nonce, Encrypt);
return { Encrypt, MsgSignature, TimeStamp, Nonce };
}
}
exports.default = default_1;
//# sourceMappingURL=WechatCrypto.js.map
;