lib/alipay/sign.rb in alipay-0.5.0 vs lib/alipay/sign.rb in alipay-0.6.0.beta1

- old
+ new

@@ -3,55 +3,70 @@ require 'base64' module Alipay module Sign def self.generate(params) - query = params.sort.map { |item| item.join('=') }.join('&') - Digest::MD5.hexdigest("#{query}#{Alipay.key}") - end - - def self.verify?(params) params = Utils.stringify_keys(params) - params.delete('sign_type') - sign = params.delete('sign') + sign_type = params.delete('sign_type') || Alipay.sign_type + key = params.delete('key') || Alipay.key - generate(params) == sign + case sign_type + when 'MD5' + generate_md5(key, params) + when 'RSA' + raise NotImplementedError, "RSA sign is unimplemented" + when 'DSA' + raise NotImplementedError, "DSA sign is unimplemented" + else + raise ArgumentError, "wrong sign_type #{sign_type}, allow values: 'MD5', 'RSA', 'DSA'" + end end - module Wap - SORTED_VERIFY_PARAMS = %w( service v sec_id notify_data ) + def self.generate_md5(key, params) + Digest::MD5.hexdigest("#{params_to_string(params)}#{key}") + end - def self.verify?(params) - params = Utils.stringify_keys(params) + def self.params_to_string(params) + params.sort.map { |item| item.join('=') }.join('&') + end - query = SORTED_VERIFY_PARAMS.map do |key| - "#{key}=#{params[key]}" - end.join('&') + def self.verify?(params) + params = Utils.stringify_keys(params) - params['sign'] == Digest::MD5.hexdigest("#{query}#{Alipay.key}") + sign_type = params.delete('sign_type') + + case sign_type + when 'MD5' + verify_md5?(params) + when 'RSA' + verify_rsa?(params) + when 'DSA' + raise NotImplementedError, "DSA verify is unimplemented" + else + raise ArgumentError, "wrong sign_type #{sign_type}, allow values: 'MD5', 'RSA', 'DSA'" end end - module App - # Alipay public key - PEM = "-----BEGIN PUBLIC KEY-----\n" \ - "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRA\n" \ - "FljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQE\n" \ - "B/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5Ksi\n" \ - "NG9zpgmLCUYuLkxpLQIDAQAB\n" \ - "-----END PUBLIC KEY-----" + def self.verify_md5?(params) + key = params.delete('key') || Alipay.key + sign = params.delete('sign') + generate_md5(key, params) == sign + end - def self.verify?(params) - params = Utils.stringify_keys(params) + ALIPAY_RSA_PUBLIC_KEY = <<-EOF +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRA +FljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQE +B/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5Ksi +NG9zpgmLCUYuLkxpLQIDAQAB +-----END PUBLIC KEY----- + EOF - pkey = OpenSSL::PKey::RSA.new(PEM) - digest = OpenSSL::Digest::SHA1.new + def self.verify_rsa?(params) + pkey = OpenSSL::PKey::RSA.new(ALIPAY_RSA_PUBLIC_KEY) + digest = OpenSSL::Digest::SHA1.new + sign = params.delete('sign') - params.delete('sign_type') - sign = params.delete('sign') - to_sign = params.sort.map { |item| item.join('=') }.join('&') - - pkey.verify(digest, Base64.decode64(sign), to_sign) - end + pkey.verify(digest, Base64.decode64(sign), params_to_string(params)) end end end