Module: WechatPayment::Sign
- Defined in:
- lib/wechat_payment/sign.rb
Constant Summary collapse
- SIGN_TYPE_MD5 =
'MD5'
- SIGN_TYPE_HMAC_SHA256 =
'HMAC-SHA256'
Class Method Summary collapse
Class Method Details
.generate(params, sign_type = SIGN_TYPE_MD5) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/wechat_payment/sign.rb', line 9 def self.generate(params, sign_type = SIGN_TYPE_MD5) key = params.delete(:key) new_key = params["key"] #after key = params.delete("key") if params["key"] #after query = params.sort.map do |k, v| "#{k}=#{v}" if v.to_s != '' end.compact.join('&') string_sign_temp = "#{query}&key=#{key || new_key || WechatPayment.key}" #after if sign_type == SIGN_TYPE_MD5 Digest::MD5.hexdigest(string_sign_temp).upcase elsif sign_type == SIGN_TYPE_HMAC_SHA256 OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, string_sign_temp).upcase else warn("WxPay Warn: unknown sign_type : #{sign_type}") end end |
.verify?(params, options = {}) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/wechat_payment/sign.rb', line 30 def self.verify?(params, = {}) params = params.dup params["appid"] = [:appid] if [:appid] params["mch_id"] = [:mch_id] if [:mch_id] params["key"] = [:key] if [:key] sign = params.delete('sign') || params.delete(:sign) generate(params, [:sign_type] || SIGN_TYPE_MD5) == sign end |