typed-wx-api
Version:
Typed Wechat API
73 lines • 3.09 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WxCrypto = void 0;
const crypto_1 = __importDefault(require("crypto"));
const pkcs7_encode_1 = require("./pkcs7_encode");
/**
* @internal
*/
class WxCrypto {
static createNonceStr() {
return Math.random().toString(36).substr(2, 15);
}
static createTimestamp() {
return new Date().getTime() / 1000 + '';
}
constructor(config) {
this.token = config.token;
this.id = config.id;
this.key = new Buffer(config.encodingAESKey + '=', 'base64');
if (this.key.length !== 32) {
throw new Error('invalided encodingAESKey');
}
this.iv = this.key.slice(0, 16);
}
signature(args) {
const shaSum = crypto_1.default.createHash('sha1');
const arr = [this.token, args.timestamp, args.nonce, args.encrypt].sort();
shaSum.update(arr.join(''));
return shaSum.digest('hex');
}
decrypt(text) {
// 创建解密对象,AES采用CBC模式,数据采用PKCS#7填充;IV初始向量大小为16字节,取AESKey前16字节
const decipher = crypto_1.default.createDecipheriv('aes-256-cbc', this.key, this.iv);
decipher.setAutoPadding(false);
let deciphered = Buffer.concat([
decipher.update(text, 'base64'),
decipher.final()
]);
deciphered = pkcs7_encode_1.PKCS7Encoder.decode(deciphered);
// 算法:AES_Encrypt[random(16B) + msg_len(4B) + msg + $CorpID]
// 去除16位随机数
const content = deciphered.slice(16);
const length = content.slice(0, 4).readUInt32BE(0);
return {
message: content.slice(4, length + 4).toString(),
id: content.slice(length + 4).toString()
};
}
encrypt(text) {
// 算法:AES_Encrypt[random(16B) + msg_len(4B) + msg + $CorpID]
// 获取16B的随机字符串
const randomString = crypto_1.default.pseudoRandomBytes(16);
const msg = new Buffer(text);
// 获取4B的内容长度的网络字节序
const msgLength = new Buffer(4);
msgLength.writeUInt32BE(msg.length, 0);
const id = new Buffer(this.id);
const bufMsg = Buffer.concat([randomString, msgLength, msg, id]);
// 对明文进行补位操作
const encoded = pkcs7_encode_1.PKCS7Encoder.encode(bufMsg);
// 创建加密对象,AES采用CBC模式,数据采用PKCS#7填充;IV初始向量大小为16字节,取AESKey前16字节
const cipher = crypto_1.default.createCipheriv('aes-256-cbc', this.key, this.iv);
cipher.setAutoPadding(false);
const cipheredMsg = Buffer.concat([cipher.update(encoded), cipher.final()]);
// 返回加密数据的base64编码
return cipheredMsg.toString('base64');
}
}
exports.WxCrypto = WxCrypto;
//# sourceMappingURL=crypto_common.js.map