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 🙏

© 2024 – Pkg Stats / Ryan Hefner

wechat-encrypt

v1.1.1

Published

微信开放平台会话消息加解密工具

Downloads

43

Readme

wechat-encrypt

微信开放平台会话消息加解密模块。

示例代码

npm install wechat-encrypt
const WechatEncrypt = require('wechat-encrypt')

const wechatEncrypt = new WechatEncrypt({
    appId: 'wx013591feaf25uoip',
    encodingAESKey: 'abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg0',
    token: 'test token'
})

// 报文主体中 Encrypt 字段的值
let encrypt = 'elJAUQEY0yKnbLbmXYdacAoDEmJlzdMeB3ryWEtNOQnJ2n1h9Y0ocSYYsW8YsrVrWhJrZe4gKKrzMs1JBCHFNHlFYCMBigDMU41WGxjwulsLjglXd+Cr7Mq/RV7TUwkkqX9+y0KmIIqAl+qYJUnuYvaug5bBMcikP9kDj3OzQ41Oppt0hzNGq7tw6RFplSW75ItMVY6Vi0d+NJTLuvIWwQqDIytcVJnNQFHOTRmm9sUVVm0kNiQp7sQljoif+j/JjMkB1fQXtrwUkLup0ql4vGZ8/126qWFR8p8tmzbDm4U/tdgLYLnEv7XFMT6cmYprmEz3cyN2yWuRfKcCBOgKyUfEt+NYwnE+1l5QK2nbOkMqorqmvc66zo0VYVj4o8nV+laMy3Celz3rDUAJMKXk/FN8ZjOsyn7sDJlo8iAhHtg='
let timestamp = '1565268520' // 推送消息链接上的 timestamp 字段值
let nonce = '331748743'	// 推送消息链接上的 nonce 字段值
let msg_signature = 'f0d525f5e849b1cd8f628eff2121b4d16765b7f2' // 推送消息链接上 msg_signature 字段值

// 校验消息是否来自微信:取链接上的 timestamp, nonce 字段和报文主体的 Encrypt 字段的值,来生成签名
// 生成的签名和链接上的 msg_signature 字段值进行对比
let signature = wechatEncrypt.genSign({ timestamp, nonce, encrypt })
let isValid = signature === msg_signature
console.log(`该消息${isValid ? '有效' : '无效'}\n\n`)
/*
该消息有效

*/

// 解密消息内容。取报文主体的 Encrypt 字段的值进行解密
let xml = wechatEncrypt.decode(encrypt)
console.log(`解密后的消息:\n${xml}\n\n`)
/*
解密后的消息:
<xml><ToUserName><![CDATA[gh_fd189404d989]]></ToUserName><FromUserName><![CDATA[o9uKB5hniJXLYJTtfjxMSSmo477k]]></FromUserName><CreateTime>1565266686</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[Hello world]]></Content><MsgId>22409229427342621</MsgId></xml>
*/


// 加密消息。调用 encode 方法,传入待加密的内容,返回加密后的结果
let encryptedMsg = wechatEncrypt.encode(xml)
console.log(`加密后的结果:\n${encryptedMsg}\n\n`)
/*
加密后的结果:
uF/fQ1LOkmHC4defoc2+h1LxRFXh2dGu4CS71Nm7I2BrWglcchikzFJw1RN9ZsylVyow1kGBj7p9Mrg0m6VGdrSKZ/aCg04Yu9lCCY7YPukf7VpBR+iK8JNiproQTdnXWREar2UPWM06aGPmQTfVcjEN1K5oMA0tOxRFt2jDtjhwCptXw7qPALCT8fJILkjL7z8e//dMCtrrxeh0NENf3oM1AqZq7ZJ/iWHfCPp+hcxNJrNZlzLgKlIuFxb8QwppvA8KyOItM+RZkr286e1hPJqnCpelXrl9MzigrnGH+BjegkQQNrHBco093vrElrOJxYnlJwHOtr/kN54nngFal/Gn1+PRCrvVPxRKE2e/pwTbCMtUbVB+W3FKTnbGDfEvzBJzrPKYmT2Woio3hTjsYEb8Qk/fMc8A8myalD3CD8pjTAY0/dTmo3Iq4jwrhwQ9HnvGxliwZ25lWRxplwQDf4aB6kngfC4tZnrNuDKewUyWr7RKrpGjxV9OtzzbZBaa
*/

WechatEncrypt(params)

构造函数

let wechatEncrypt = new WechatEncrypt({ appId, token, encodingAESKey })

实例方法:

encode(msg)

加密消息

let encryptedMsg = wechatEncrypt.encode(rawMsg)

decode(msg)

解密消息

let decryptedMsg = wechatEncrypt.decode(encryptedMsg)

genSign(params)

生成签名 signature。用于校验消息是否来自微信。回复消息时也需要生成签名。

let signature = wechatEncrypt.genSign({ timestamp, nonce, encrypt })