Module: AlipayMini::Sign

Defined in:
lib/alipay_mini/sign.rb,
lib/alipay_mini/sign/rsa2.rb

Defined Under Namespace

Classes: RSA2

Class Method Summary collapse

Class Method Details

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

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/alipay_mini/sign.rb', line 38

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

  sign_type = params.fetch('sign_type')
  sign = params.delete('sign')
  public_key = options[:public_key] || AlipayMini.config[:public_key]
  params.delete('sign_type') if options.fetch(:delete_sign_type, true)

  string = AlipayMini::Utils.params_to_string(params)

  case sign_type
  when 'RSA2'
    RSA2.verify?(public_key, string, sign)
  else
    false
  end
end

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



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

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

  sign_type = options[:sign_type] || AlipayMini.config[:sign_type]
  key = options[:private_key] || AlipayMini.config[:private_key]

  string = Utils.params_to_string(params)

  case sign_type
  when 'RSA2'
    RSA2.sign(key, string)
  else
    raise ArgumentError, "invalid sign_type #{sign_type}, allowed type: 'RSA2'"
  end
end

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

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alipay_mini/sign.rb', line 20

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

  sign_type = params.delete('sign_type')
  sign = params.delete('sign')
  public_key = options[:public_key] || AlipayMini.config[:public_key]

  # 转义正斜杠,防止验签无法通过
  string = params.to_json.gsub('/', '\/')

  case sign_type
  when 'RSA2'
    RSA2.verify?(public_key, string, sign)
  else
    false
  end
end