All Projects → pengng → wechat-encrypt

pengng / wechat-encrypt

Licence: other
微信开放平台会话消息加解密模块

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to wechat-encrypt

Jpagerslidingtabstrip
🔥A useful tablayout modify from astuetz/PagerSlidingTabStrip
Stars: ✭ 233 (+832%)
Mutual labels:  message
WatsonCluster
A simple C# class using Watson TCP to enable a one-to-one high availability cluster.
Stars: ✭ 18 (-28%)
Mutual labels:  message
MessageTip
轻快型消息提示窗
Stars: ✭ 54 (+116%)
Mutual labels:  message
advancedSmsManager
Advanced SmsManager is avery handy library for sending sms for single and two sim-card phones with many options.
Stars: ✭ 27 (+8%)
Mutual labels:  message
Message-Manager-Bot
A Telegram Message Manager Bot by @AbirHasan2005
Stars: ✭ 32 (+28%)
Mutual labels:  message
telegram-logger-errors
Telegram logger errors package laravel | Laravel пакет telegram логгер ошибок
Stars: ✭ 15 (-40%)
Mutual labels:  message
Linphone Desktop
Linphone is a free VoIP and video softphone based on the SIP protocol. Mirror of git://git.linphone.org/linphone-desktop.git
Stars: ✭ 212 (+748%)
Mutual labels:  message
WatsonWebsocket
A simple C# async websocket server and client for reliable transmission and receipt of data
Stars: ✭ 158 (+532%)
Mutual labels:  message
tasker-config
My very own tasker configuration
Stars: ✭ 28 (+12%)
Mutual labels:  message
SimpleDialogs
💬 A simple framework to help displaying dialogs on a WPF app
Stars: ✭ 24 (-4%)
Mutual labels:  message
qless-php
PHP Bindings for qless
Stars: ✭ 25 (+0%)
Mutual labels:  message
SHPopup
A lightweight library for popup view
Stars: ✭ 36 (+44%)
Mutual labels:  message
prophetjs
Vanilla JS library to display toast messages.
Stars: ✭ 31 (+24%)
Mutual labels:  message
Jest Expect Message
Add custom message to Jest expects 🃏🗯
Stars: ✭ 240 (+860%)
Mutual labels:  message
message
基于 Vue2.x 的消息提示组件。Vue-based message component
Stars: ✭ 26 (+4%)
Mutual labels:  message
Twitch Js
A community-centric, community-supported version of tmi.js
Stars: ✭ 225 (+800%)
Mutual labels:  message
vercel-toast
💬 Framework-agnostic vercel design's toast component (≈1KB Gzipped)
Stars: ✭ 67 (+168%)
Mutual labels:  message
xyTalk-pc
企业IM即时通讯定制平台,百万级高并发、高性能、可扩展、安全、高交互体验的企业通信和协作im平台。包含通讯服务、客户端(PC、Android、iOS)、Web门户(用于集成企业应用)、WebAPI。
Stars: ✭ 48 (+92%)
Mutual labels:  message
socket
Dazzle Async Socket
Stars: ✭ 19 (-24%)
Mutual labels:  message
super-mario-message
Display custom messages in a Super Mario Bros environment
Stars: ✭ 18 (-28%)
Mutual labels:  message

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 })
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].