Skip to content
Lua version of the WeChat Message Cryptography
Branch: feature/use-or…
Clone or download
Pull request Compare This branch is 6 commits ahead of hello2t:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/resty
README.md

README.md

WXBizMsgCrypt

the WeChat Message Cryptography in the openresty lua verison.

aes_pad.lua

extend the aes.lua with a set_padding method, discussing here

crypt.lua

encrypt(text, timestamp, nonce)

decrypt(text_encrypted)

get_sha1({token, timestamp, nonce, text_encrypted})

Synopsis

# nginx.conf:

server {
    location = /test {
        content_by_lua_block {
            local token = "pamtest"
            local aesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"
            local timestamp = "1409304348"
            local nonce = "xxxxxx"
            local appId = "wxb11529c136998cb6"
            local sample = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>"

            local crypt = require "resty.crypt"
            local wxmc = crypt:new(token, aesKey, appId)
            local encrypted = wxmc:encrypt(sample, timestamp, nonce)
            ngx.say("the sample encrypted: ", encrypted)

            local gsub = ngx.re.gsub
            sample = gsub(encrypted, [=[.*<Encrypt>(?:<!\[CDATA\[)?(.*?)(?:\]\]>)?</Encrypt>.*]=], "$1")
            ngx.say("the sample decrypted: ", wxmc:decrypt(sample))
        }
    }
}
You can’t perform that action at this time.