Sha256: 0890b512ad2a43d805a6ea2390b53e4a4146be2e48d78bd95bd5a9cadcef022f

Contents?: true

Size: 1.44 KB

Versions: 17

Compression:

Stored size: 1.44 KB

Contents

module Alipay
  module Sign
    def self.generate(params, options = {})
      params = Utils.stringify_keys(params)
      sign_type = options[:sign_type] || Alipay.sign_type
      key = options[:key] || Alipay.key
      string = params_to_string(params)

      case sign_type
      when 'MD5'
        MD5.sign(key, string)
      when 'RSA'
        RSA.sign(key, string)
      when 'DSA'
        DSA.sign(key, string)
      else
        raise ArgumentError, "invalid sign_type #{sign_type}, allow value: 'MD5', 'RSA', 'DSA'"
      end
    end

    ALIPAY_RSA_PUBLIC_KEY = <<-EOF
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRA
FljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQE
B/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5Ksi
NG9zpgmLCUYuLkxpLQIDAQAB
-----END PUBLIC KEY-----
    EOF

    def self.verify?(params, options = {})
      params = Utils.stringify_keys(params)

      sign_type = params.delete('sign_type')
      sign = params.delete('sign')
      string = params_to_string(params)

      case sign_type
      when 'MD5'
        key = options[:key] || Alipay.key
        MD5.verify?(key, string, sign)
      when 'RSA'
        RSA.verify?(ALIPAY_RSA_PUBLIC_KEY, string, sign)
      when 'DSA'
        DSA.verify?(string, sign)
      else
        false
      end
    end

    def self.params_to_string(params)
      params.sort.map { |item| item.join('=') }.join('&')
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
alipay-0.17.0 lib/alipay/sign.rb
alipay-0.16.0 lib/alipay/sign.rb
alipay-0.15.2 lib/alipay/sign.rb
alipay-0.15.1 lib/alipay/sign.rb
alipay-0.15.0 lib/alipay/sign.rb
yl_alipay-0.15.3 lib/alipay/sign.rb
yl_alipay-0.15.2 lib/alipay/sign.rb
alipay-0.14.0 lib/alipay/sign.rb
alipay-0.13.0 lib/alipay/sign.rb
alipay-0.12.0 lib/alipay/sign.rb
alipay-0.11.0 lib/alipay/sign.rb
alipay-0.10.0 lib/alipay/sign.rb
alipay-0.9.0 lib/alipay/sign.rb
alipay-0.8.0 lib/alipay/sign.rb
alipay-0.7.1 lib/alipay/sign.rb
alipay-0.7.0 lib/alipay/sign.rb
alipay-0.6.0 lib/alipay/sign.rb