Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Created July 19, 2014 02:18
Show Gist options
  • Select an option

  • Save zhangyuan/c795c8803cabe1f52d83 to your computer and use it in GitHub Desktop.

Select an option

Save zhangyuan/c795c8803cabe1f52d83 to your computer and use it in GitHub Desktop.
Wechat Signature Validator
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