Skip to content

Instantly share code, notes, and snippets.

@bitristan
Last active January 31, 2018 23:50
Show Gist options
  • Save bitristan/10885044 to your computer and use it in GitHub Desktop.
Save bitristan/10885044 to your computer and use it in GitHub Desktop.
open wechat verify with node js
var url = require('url'),
crypto = require('crypto'),
querystring = require('querystring'),
http = require('http'),
port = 18080;
var token = '***your token';
http.createServer(function(req, res) {
if (req.method === 'GET') {
var queryObj = querystring.parse(url.parse(req.url).query);
var signature = queryObj.signature,
timestamp = queryObj.timestamp,
nonce = queryObj.nonce,
echostr = queryObj.echostr;
var sha1 = crypto.createHash('sha1'),
sha1Str = sha1.update([token, timestamp, nonce].sort().join('')).digest('hex');
console.log("my signature: " + sha1Str);
res.writeHead(200, {'Content-Type': 'text/plain'});
return res.end((sha1Str === signature) ? echostr : '');
}
res.end();
}).listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment