From 23f33a52d6cfa48aea6c7cc994ef611568ed6a4a Mon Sep 17 00:00:00 2001 From: salamander Date: Wed, 9 Dec 2020 21:57:47 +0800 Subject: [PATCH 1/3] add decrypt data function for wechat miniprogram. --- lib/util.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/util.js b/lib/util.js index 1f9341f..91b1f5a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,5 @@ 'use strict'; +const crypto = require('crypto'); /*! * 对提交参数一层封装,当POST JSON,并且结果也为JSON时使用 */ @@ -42,3 +43,30 @@ function _replaceOneChar(c) { exports.replaceJSONCtlChars = function (str) { return str.replace(JSONCtlCharsRE, _replaceOneChar); }; + +/** + * 小程序加密数据解密算法 + * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html + * @param {string} sessionKey + * @param {string} iv + * @param {string} encryptedData + * @returns {Promise} + */ +exports.decryptDataForMiniProgram = async function (sessionKey, iv, encryptedData) { + const sessionKeyBuffer = Buffer.from(sessionKey, 'base64'); + const ivBuffer = Buffer.from(iv, 'base64'); + const encryptedBuffer = Buffer.from(encryptedData, 'base64'); + + try { + const decipher = crypto.createDecipheriv('aes-128-cbc', sessionKeyBuffer, ivBuffer); + // 设置自动 padding 为 true,删除填充补位 + decipher.setAutoPadding(true); + const decoded = + decipher.update(encryptedBuffer, 'binary', 'utf8') + + decipher.final('utf8'); + + return JSON.parse(decoded); + } catch (err) { + throw new Error('解密失败'); + } +}; From 570d3cd4cd41179f4efa5ae0d6b6895541fa3c5d Mon Sep 17 00:00:00 2001 From: salamander Date: Wed, 9 Dec 2020 22:00:52 +0800 Subject: [PATCH 2/3] fix. --- lib/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 91b1f5a..2efd3ae 100644 --- a/lib/util.js +++ b/lib/util.js @@ -50,9 +50,9 @@ exports.replaceJSONCtlChars = function (str) { * @param {string} sessionKey * @param {string} iv * @param {string} encryptedData - * @returns {Promise} + * @returns {Object} */ -exports.decryptDataForMiniProgram = async function (sessionKey, iv, encryptedData) { +exports.decryptDataForMiniProgram = function (sessionKey, iv, encryptedData) { const sessionKeyBuffer = Buffer.from(sessionKey, 'base64'); const ivBuffer = Buffer.from(iv, 'base64'); const encryptedBuffer = Buffer.from(encryptedData, 'base64'); From 537465d0a7810e34094df45b6fa63f651da43ac9 Mon Sep 17 00:00:00 2001 From: salamander Date: Wed, 24 Mar 2021 14:42:28 +0800 Subject: [PATCH 3/3] not to request token. --- lib/api_common.js | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/api_common.js b/lib/api_common.js index 34c92f7..60156d8 100644 --- a/lib/api_common.js +++ b/lib/api_common.js @@ -216,7 +216,7 @@ class API { err.code = 40001; throw err; } - return this.getAccessToken(); + throw new Error('can\'t set access token'); } } diff --git a/package.json b/package.json index 12d800f..29fa0f0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "co-wechat-api", + "name": "co-wechat-api-new", "version": "3.11.0", "description": "微信公共平台Node库API,ES6版本", "main": "index.js", @@ -30,7 +30,7 @@ "rewire": "*", "travis-cov": "*" }, - "author": "Jackson Tian", + "author": "Salamander", "license": "MIT", "readmeFilename": "README.md", "directories": {