npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

weixin-crypto

v2.0.0

Published

微信公众平台消息加解密库

Readme

weixin-crypto

npm issues pulls license

微信公众平台消息和开放数据加解密库,可用于微信公众号、微信小程序、微信开放平台等。

具体请参考:

特性

  • 零依赖
  • 支持 TypeScript
  • 不限制 XML 或 JSON

使用

安装

# npm
npm install weixin-crypto
# yarn
yarn add weixin-crypto

消息加解密

初始化

import { WXBizMsgCrypt } from 'weixin-crypto';

const wxCrypto = new WXBizMsgCrypt({
  appid: '公众平台的 appid',
  token: '接收消息的校验 token',
  encodingAESKey: '接收消息的 EncodingAESKey',
});

校验消息体签名

// 判断 URL 上的 msg_signature 和计算出的是否一致,伪代码
const timestamp = 'URL 上的 timestamp 参数';
const nonce = 'URL 上的 nonce 参数';
const msg_signature = 'URL 上的 msg_signature 参数';
const msg_encrypt = '请求体中的密文消息';

if (wxCrypto.getSignature({ timestamp, nonce, msg_encrypt }) === msg_signature) {
  // 消息体签名校验通过
}

msg_encrypt 为请求体中的密文消息,如果为 XML 格式,需要先自行解析出来,weixin-crypto 不会帮你解析 XML 内容。

解密消息

const msg_encrypt = '请求体中的密文消息';

// 解密后的内容
const decrypted = wxCrypto.decrypt(msg_encrypt);

解密前需要自行校验消息体签名,解密过程不包含签名校验流程;如果请求体为 XML 格式,解密后的内容为 XML 字符串,需要自行解析 XML 内容。

加密消息

const content = '要加密的内容';

// 解密后的内容
const encrypted = wxCrypto.encrypt(content);

如果要加密 XML 格式的内容,需要先自行拼接成 XML 字符串再进行加密。

开放数据解密

初始化

import { WXBizDataCrypt } from 'weixin-crypto';

const wxCrypto = new WXBizDataCrypt({
  appid: '公众平台的 appid',
  sessionKey: '用户登录的 session_key',
});

解密数据

const msg_encrypt = '请求体中的密文消息';

// 解密后的内容
const decrypted = wxCrypto.decrypt({
  encryptedData: '包括敏感数据在内的完整用户信息的加密数据',
  iv: '加密算法的初始向量',
});

License

MIT License (c) 2020-preset pengtikui