1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| const crypto = require('crypto'); const { getConfig } = require('../utils/getConfig'); const { includes } = require('lodash'); const parseString = require('xml2js').parseString;
class PKCS7 {
decode(text) { let pad = text[text.length - 1] if (pad < 1 || pad > 32) { pad = 0 } return text.slice(0, text.length - pad) }
encode(text) { const blockSize = 32 const textLength = text.length const amountToPad = blockSize - (textLength % blockSize) const result = Buffer.alloc(amountToPad) result.fill(amountToPad) return Buffer.concat([text, result]) } }
class WXMsgCrypto {
constructor(token, encodingAESKey, appId) { if (!token || !encodingAESKey || !appId) { throw new Error('please check arguments') } this.token = token this.appId = appId
let AESKey = Buffer.from(encodingAESKey + '=', 'base64') if (AESKey.length !== 32) { throw new Error('encodingAESKey invalid') } this.key = AESKey this.iv = AESKey.slice(0, 16) this.pkcs7 = new PKCS7() }
getSignature(timestamp, nonce, encrypt) { const sha = crypto.createHash('sha1') const arr = [this.token, timestamp, nonce, encrypt].sort() sha.update(arr.join('')) return sha.digest('hex') }
decrypt(text) { const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, this.iv) decipher.setAutoPadding(false)
let deciphered = Buffer.concat([decipher.update(text, 'base64'), decipher.final()])
deciphered = this.pkcs7.decode(deciphered) const content = deciphered.slice(16) const length = content.slice(0, 4).readUInt32BE(0)
return { message: content.slice(4, length + 4).toString(), appId: content.slice(length + 4).toString() } }
encrypt(text) { const randomString = crypto.pseudoRandomBytes(16)
const msg = Buffer.from(text) const msgLength = Buffer.alloc(4) msgLength.writeUInt32BE(msg.length, 0)
const id = Buffer.from(this.appId)
const bufMsg = Buffer.concat([randomString, msgLength, msg, id])
const encoded = this.pkcs7.encode(bufMsg)
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, this.iv) cipher.setAutoPadding(false)
const cipheredMsg = Buffer.concat([cipher.update(encoded), cipher.final()])
return cipheredMsg.toString('base64') } }
const { token, WechatAppID, EncodingAESKey } = getConfig();
const wxmc = new WXMsgCrypto(token, EncodingAESKey, WechatAppID)
function WXMsgCryptoMiddleware(req, res, next) { if (includes(req.path, '/wechatAccess') && req.method.toLowerCase() === 'post') { let query = req.query let xml = req.body.xml let msgSignature = wxmc.getSignature(query.timestamp, query.nonce, xml.encrypt) if (msgSignature !== query.msg_signature) { res.send('服务器出错,请稍后重试!'); } else { parseString(wxmc.decrypt(xml.encrypt).message, { explicitArray: false }, function(err, res) { req.body.xml = res.xml next() }) } } else { next(); } }
module.exports = WXMsgCryptoMiddleware;
|