UNPKG

wx-ding-aes

Version:

Wechat open platform, Enterprise wechat and DingTalk AES decode library.

35 lines (34 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const aes_1 = require("./aes"); const utils_1 = require("./utils"); /** * @description 加密方法 * @author beary * @param text 需要加密的文本 * @param encodingAESKey AES 秘钥 * @param key 放在加密内容的尾部作为校验的字符串,如果不是开发企业微信或者钉钉,则不是必须的 */ exports.encode = (text, encodingAESKey, key) => { const textBuffer = Buffer.from(text); const datas = [ Buffer.from(utils_1.randomStr(16)), Buffer.from(textBuffer.length.toString(16).padStart(8, '0'), 'hex'), textBuffer ]; if (key) datas.push(Buffer.from(key)); return aes_1.encrypt(Buffer.concat(datas), encodingAESKey); }; /** * @description 解密方法 * @author beary * @param text 需要解密的密文,必须是 base64 编码的 * @param encodingAESKey AES 密钥 */ exports.decode = (text, encodingAESKey, key) => { const decryptContent = aes_1.decrypt(text, encodingAESKey); return decryptContent .slice(20, 20 + decryptContent.readUInt32BE(16)) .toString(); };