Giter VIP home page Giter VIP logo

weixin-crypto's Introduction

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

weixin-crypto's People

Contributors

pengtikui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.