Package Exports
- node-wxcrypto
Readme
安装
# 使用pnpm
$ pnpm i node-wxcrypto
# 使用yarn
$ yarn add node-wxcrypto使用
在实例化和加解密方法均支持传入 options:
normalizeTags,buildXmlOptions,xmlOptions,加解密方法里面传入的 options 优先级更高。
normalizeTags支持将 xml 属性由驼峰转下划线分隔的小写形式;buildXmlOptions透传用于生成 xml 字符串的配置;xmlOptions透传用于解析 xml 字符串的配置。
注意:
normalizeTags会全量覆盖xmlOptions里面的tagNameProcessors方法,如果想要自定义tagNameProcessors,请不要传入normalizeTags
引入和使用
- require 引入
const { WxCrypto } = require('node-wxcrypto')
/**
* class WxCrypto
*
* @param {string} token 消息校验Token,开发者在代替公众号或小程序接收到消息时,用此Token来校验消息。
* @param {string} aesKey 消息加解密Key,在代替公众号或小程序收发消息过程中使用。必须是长度为43位的字符串,只能是字母和数字。
* @param {string} appID 小程序appID
* @param {object} options Options
* @return {Object} WxCrypto instance
*/
const wxCrypto = new WxCrypto(token, aesKey, appID, options)
/**
* decrypt data
*
* @param {string} encrypt encrypt data
* @param {string} timestamp timestamp
* @param {string} nonce nonce
* @param {object} options Options
* @return {Object} decrypt data
*/
const data = await wxCrypto.decrypt(encrypt, timestamp, nonce, options)- import 引入
import {
// PKCS7Decode,
// PKCS7Encode,
WxCrypto
// aes256Decrypt,
// aes256Encrypt,
// buildXML,
// buildXMLSync,
// parseXML,
// parseXMLSync,
// sha1
} from 'node-wxcrypto'
const wxCrypto = new WxCrypto(token, aesKey, appID, options)
const data = await wxCrypto.decrypt(encrypt, timestamp, nonce, options)使用配置
持将 xml 属性由驼峰转下划线分隔的小写形式:ComponentVerifyTicket => component_verify_ticket
// normalizeTags可传入布尔值或者字符串,传入字符串时使用该字符串分隔,例如:normalizeTags = "__",得到:`ComponentVerifyTicket => component__verify__ticket`
const wxCrypto = new WxCrypto(token, aesKey, appID, {
normalizeTags: true,
buildXmlOptions: {}, // 透传用于生成 xml 字符串的配置
xmlOptions: {} // 透传用于解析 xml 字符串的配置
})
const data = await wxCrypto.decrypt(encrypt, timestamp, nonce, options)问题和支持
Please open an issue here.