Created
July 19, 2014 02:18
-
-
Save zhangyuan/c795c8803cabe1f52d83 to your computer and use it in GitHub Desktop.
Wechat Signature Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Wechat | |
| class SignatureValidator | |
| attr_accessor :token, :params | |
| def initialize(token, params) | |
| @token = token | |
| @params = params | |
| end | |
| def calculate | |
| require 'openssl' unless defined?(OpenSSL) | |
| text = %w(timestamp nonce).map {|name| params[name].to_s}.push(token).sort.join | |
| Digest::SHA1.hexdigest text | |
| end | |
| def valid? | |
| calculate == params['signature'] | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment