Module: Alipay::Sign

Defined in:
lib/alipay/sign.rb,
lib/alipay/sign/dsa.rb,
lib/alipay/sign/md5.rb,
lib/alipay/sign/rsa.rb,
lib/alipay/sign/rsa2.rb

Defined Under Namespace

Classes: DSA, MD5, RSA, RSA2

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.generate(params, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/alipay/sign.rb', line 3

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

.params_to_string(params) ⇒ Object



50
51
52
# File 'lib/alipay/sign.rb', line 50

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

.verify?(params, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/alipay/sign.rb', line 30

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