This commit is contained in:
Xu Chang 2022-05-06 19:33:03 +08:00
parent 1fae8965bf
commit c3399f36b0
2 changed files with 28 additions and 1 deletions

View File

@ -7,5 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "XuChang",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"@types/node": "^17.0.31"
}
}

View File

@ -1,3 +1,6 @@
import crypto from 'crypto';
import { Buffer } from 'buffer';
export class WechatMpInstance {
appId: string;
appSecret: string;
@ -18,4 +21,25 @@ export class WechatMpInstance {
unionId: unionid as string,
};
}
decryptData(sessionKey: string, encryptedData: string, iv: string, signature: string) {
const skBuf = Buffer.from(sessionKey, 'base64');
// const edBuf = Buffer.from(encryptedData, 'base64');
const ivBuf = Buffer.from(iv, 'base64');
const decipher = crypto.createDecipheriv('aes-128-cbc', skBuf, ivBuf);
// 设置自动 padding 为 true删除填充补位
decipher.setAutoPadding(true);
let decoded = decipher.update(encryptedData, 'base64', 'utf8');
decoded += decipher.final('utf8');
const data = JSON.parse(decoded);
if (data.watermark.appid !== this.appId) {
throw new Error('Illegal Buffer');
}
return data;
}
};