unionpei 0.0.5 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d164cb2fc734a86f517e21dd0216f7a3749abda62509c36a9b3b6e098bed19e7
4
- data.tar.gz: 2e88596db1f2bc6f5797cb2385442f1a092763048ba6d76d80c691d59add3c58
3
+ metadata.gz: ab2f6a36a20d9faf9d86114493a7ec077afd68a937cc8f321a9b721d72ec5765
4
+ data.tar.gz: 3430627f305f5772463bead2e8ca45b39414b023a02da1d7856433216fc2b16c
5
5
  SHA512:
6
- metadata.gz: 4a5dc0102810b1d31f93d86ccce74b2ebad95eb2c14be30e164d42bef7c0103acaceab54eb8e4fcdc2d0535feadd6257f7da88827c0a83349a4f2c4065ac8a36
7
- data.tar.gz: 46146c6496f12c51ba2cc6637d7ddc1f57a2b0ee5aca70f81432ca3ff4227e044963cbaf437c5c616851e4ea0e37e04c7812b246f2e80d834db9223323fece16
6
+ metadata.gz: 179c56aac0e48f0938de414db3a46978d3ac6d46b03cad194cbeada375c4e028778a1aa68083910ef71f39e7112538cd3999ca7d0504e4f73bc80c06bee82ab2
7
+ data.tar.gz: 7097aaa2fd90dae33c6426a583c6d5deefdf231dbfe7ca5a00f4524f5134767849e0cc50b52d4e6415e2e3ace6790337f0a17770cb9e901e68fee014ffe207b0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in alipay.gemspec
6
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,25 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEPzCCAyegAwIBAgIFEDl2NhIwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UEBhMC
3
+ Q04xMDAuBgNVBAoTJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhv
4
+ cml0eTEXMBUGA1UEAxMOQ0ZDQSBURVNUIE9DQTEwHhcNMjAwOTExMDI0MzI2WhcN
5
+ MjUwOTExMDI0MzI2WjBzMQswCQYDVQQGEwJDTjESMBAGA1UEChMJQ0ZDQSBPQ0Ex
6
+ MQ0wCwYDVQQLEwRZQ0NBMRUwEwYDVQQLEwxJbmRpdmlkdWFsLTExKjAoBgNVBAMM
7
+ IVlDQ0FA5rWL6K+V5L2/55SoQDAwMDQwMDAwOlNJR05AMTCCASIwDQYJKoZIhvcN
8
+ AQEBBQADggEPADCCAQoCggEBALUwYYpqUXZyDAu0gX5d8XkiUfFxdCan/VyLa6Cz
9
+ KH38cjX0QZIShn/O6Cw2hn2WurP/r3LdopLRzTHI0vIDJpQY/0Y135QHRFZHkAH0
10
+ omRTfAZ/atePnRF7VW766LGhR5n05h1nITDHlzCZYPSumpDPpVcJj4y30+G3A5Ou
11
+ 1VVAsuLi48XtGIKwRX6gMXI+P75RwHSmPt5/pHlEPT6wUbmF0HBoF2gRBpYZwiSK
12
+ 51Z52XUVEk96reolFFLu/9qyL767/v2izd5YuN9i7oSXNw1gDYcLnAuww6V6BUnK
13
+ Kq4KUG6H3Lz3WyXbEay72f12A5pnHWDjLEOwJ2SG1VVMLN8CAwEAAaOB9DCB8TAf
14
+ BgNVHSMEGDAWgBTPcJ1h6518Lrj3ywJA9wmd/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G9w0BAQUFAAOCAQEAM0eTkM35D4hj
16
+ RlGC63wY0h++wVPUvOrObqAVBbzEEQ7ScBienmeY8Q6lWMUTXM9ALibZklpJPcJv
17
+ 3ntht7LL6ztd4wdX7E9RzZCQnRvbL9A/BU3NxWdeSpCg/OyPod5oCKP+6Uc7kApi
18
+ F9OtYNWnt3l2Zp/NiedzEQD8H4qEWQLAq+0dFo5BkfVhb/jPcktndpfPOuH1IMhP
19
+ tVcvo6jpFHw4U/nP2Jv59osIE97KJz/SPt2JAYnZOlIDqWwp9/Afvt0/MDr8y0PK
20
+ Q9c6eqIzBx7a9LpUTUl5u1jS+xSDZ/KF2lXnjwaFp7jICLWEMlBstCoogi7KwH9A
21
+ LpJP7/dj9g==
22
+ -----END CERTIFICATE-----
Binary file
data/install.sh ADDED
@@ -0,0 +1,2 @@
1
+ gem build unionpei.gemspec
2
+ gem install unionpei-*.gem
@@ -42,7 +42,7 @@ acpsdk.ifValidateRemoteCert=false
42
42
  acpsdk.backUrl=http://222.222.222.222:8080/backRcvResponse
43
43
 
44
44
  ;前台通知地址,填写后台接收银联后台通知的地址,必须外网能访问
45
- acpsdk.frontUrl=/subscriptions
45
+ acpsdk.frontUrl=localhost:3000/subscriptions
46
46
 
47
47
  ;;;;;;;;;;;;;;;;;;;;;;;;;入网测试环境签名证书配置 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48
48
  ; 多证书的情况证书路径为代码指定,可不对此块做配置。
@@ -1,4 +1,4 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'openssl'
4
4
  require 'base64'
@@ -8,99 +8,94 @@ require_relative 'sdk_util'
8
8
 
9
9
  module UnionPei
10
10
  class AcpService
11
- def AcpService.sign(req, certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
11
+ def self.sign(req, certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
12
12
  SDKUtil.buildSignature(req, certPath, certPwd)
13
13
  end
14
14
 
15
- def AcpService.signByCertInfo(req, certPath, certPwd)
15
+ def self.signByCertInfo(req, certPath, certPwd)
16
16
  SDKUtil.buildSignature(req, certPath, certPwd)
17
17
  end
18
18
 
19
- def AcpService.signBySecureKey(req, secureKey)
19
+ def self.signBySecureKey(req, secureKey)
20
20
  SDKUtil.buildSignature(req, nil, nil, secureKey)
21
21
  end
22
22
 
23
- def AcpService.validate(resp)
23
+ def self.validate(resp)
24
24
  SDKUtil.verify(resp)
25
25
  end
26
26
 
27
- def AcpService.validateBySecureKey(resp, secureKey)
27
+ def self.validateBySecureKey(resp, secureKey)
28
28
  SDKUtil.verifyBySecureKey(resp, secureKey)
29
29
  end
30
30
 
31
- def AcpService.post(params, url)
31
+ def self.post(params, url)
32
32
  content = SDKUtil.createLinkString(params, false, true)
33
33
  respString = SDKUtil.post(url, content)
34
- resp = SDKUtil.parseQString(respString)
35
- return resp
34
+ SDKUtil.parseQString(respString)
36
35
  end
37
36
 
38
- def AcpService.createAutoFormHtml(params, reqUrl)
39
- return SDKUtil.createAutoFormHtml(params, reqUrl)
37
+ def self.createAutoFormHtml(params, reqUrl)
38
+ SDKUtil.createAutoFormHtml(params, reqUrl)
40
39
  end
41
40
 
42
- def AcpService.getCustomerInfo(customerInfo)
43
- if(customerInfo == nil or customerInfo.length == 0)
44
- return ""
45
- end
46
- return Base.encode64("{" + SDKUtil.createLinkString(customerInfo,false,false)+"}").gsub(/\n|\r/, '')
41
+ def self.getCustomerInfo(customerInfo)
42
+ return '' if customerInfo.nil? || customerInfo.length.zero?
43
+
44
+ Base.encode64("{#{SDKUtil.createLinkString(customerInfo, false, false)}}").gsub(/\n|\r/, '')
47
45
  end
48
46
 
49
- def AcpService.getCustomerInfoWithEncrypt(customerInfo)
50
- if(customerInfo == nil or customerInfo.length == 0)
51
- return ""
52
- end
47
+ def self.getCustomerInfoWithEncrypt(customerInfo)
48
+ return '' if customerInfo.nil? || customerInfo.length.zero?
49
+
53
50
  encryptedInfo = {}
54
- for key in customerInfo.keys
55
- if (key == 'phoneNo' or key == 'cvn2' or key == 'expired')
56
- encryptedInfo[key] = customerInfo.delete(key)
57
- end
51
+ customerInfo.each_key do |key|
52
+ encryptedInfo[key] = customerInfo.delete(key) if (key == 'phoneNo') || (key == 'cvn2') || (key == 'expired')
58
53
  end
59
- if (encryptedInfo.length > 0)
54
+ if encryptedInfo.length.positive?
60
55
  encryptedInfo = SDKUtil.createLinkString(encryptedInfo, false, false)
61
56
  encryptedInfo = AcpService.encryptData(encryptedInfo, SDKConfig.instance.encryptCertPath)
62
57
  customerInfo['encryptedInfo'] = encryptedInfo
63
58
  end
64
- return Base64.encode64("{" + SDKUtil.createLinkString(customerInfo,false,false)+"}").gsub(/\n|\r/, '')
59
+ Base64.encode64("{#{SDKUtil.createLinkString(customerInfo, false, false)}}").gsub(/\n|\r/, '')
65
60
  end
66
61
 
67
- def AcpService.parseCustomerInfo(customerInfostr, certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
62
+ def self.parseCustomerInfo(customerInfostr, certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
68
63
  customerInfostr = Base64.decode64(customerInfostr)
69
- customerInfostr = customerInfostr[1, customerInfostr.length-1]
64
+ customerInfostr = customerInfostr[1, customerInfostr.length - 1]
70
65
  customerInfo = SDKUtil.parseQString(customerInfostr)
71
66
  if customerInfo['encryptedInfo']
72
67
  encryptedInfoStr = customerInfo.delete('encryptedInfo')
73
68
  encryptedInfoStr = AcpService.decryptData(encryptedInfoStr, certPath, certPwd)
74
69
  encryptedInfo = SDKUtil.parseQString(encryptedInfoStr)
75
- for key in encryptedInfo.keys
70
+ encryptedInfo.each_key do |key|
76
71
  customerInfo[key] = encryptedInfo[key]
77
72
  end
78
73
  end
79
- return customerInfo
74
+ customerInfo
80
75
  end
81
76
 
82
- def AcpService.getEncryptCertId
83
- return CertUtil.getEncryptCertId
77
+ def self.getEncryptCertId
78
+ CertUtil.getEncryptCertId
84
79
  end
85
80
 
86
- def AcpService.encryptData(data, certPath=SDKConfig.instance.encryptCertPath)
87
- return SDKUtil.encryptPub(data, certPath)
81
+ def self.encryptData(data, certPath = SDKConfig.instance.encryptCertPath)
82
+ SDKUtil.encryptPub(data, certPath)
88
83
  end
89
84
 
90
- def AcpService.decryptData(data, certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
91
- return SDKUtil.decryptPri(data, certPath, certPwd)
85
+ def self.decryptData(data, certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
86
+ SDKUtil.decryptPri(data, certPath, certPwd)
92
87
  end
93
88
 
94
- def AcpService.deCodeFileContent(params, fileDirectory)
95
- return SDKUtil.deCodeFileContent(params, fileDirectory)
89
+ def self.deCodeFileContent(params, fileDirectory)
90
+ SDKUtil.deCodeFileContent(params, fileDirectory)
96
91
  end
97
92
 
98
- def AcpService.enCodeFileContent(path)
99
- return SDKUtil.enCodeFileContent(path)
93
+ def self.enCodeFileContent(path)
94
+ SDKUtil.enCodeFileContent(path)
100
95
  end
101
96
 
102
- def AcpService.updateEncryptCert(params)
103
- return SDKUtil.getEncryptCert(params)
97
+ def self.updateEncryptCert(params)
98
+ SDKUtil.getEncryptCert(params)
104
99
  end
105
100
  end
106
101
  end
@@ -1,37 +1,31 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'openssl'
4
4
  require 'base64'
5
5
  require_relative 'log_util'
6
6
  require_relative 'sdk_config'
7
7
 
8
-
9
8
  module UnionPei
10
- UNIONPAY_CNNAME = "中国银联股份有限公司"
9
+ UNIONPAY_CNNAME = '中国银联股份有限公司'
11
10
 
12
11
  class Cert
13
12
  attr_accessor :cert, :certId, :key
14
- @certId
15
- @key
16
- @cert
17
13
  end
18
14
 
19
15
  class CertUtil
20
-
21
16
  @@signCerts = {}
22
17
  @@encryptCert = {}
23
- @@verifyCerts = {} #5.0.0验签证书,key是certId
24
- @@verifyCerts5_1_0 = {} #5.1.0验签证书,key是base64的证书内容
18
+ @@verifyCerts = {} # 5.0.0验签证书,key是certId
19
+ @@verifyCerts5_1_0 = {} # 5.1.0验签证书,key是base64的证书内容
25
20
  @@middleCert = nil
26
21
  @@rootCert = nil
27
22
 
28
- private
29
- def CertUtil.initSignCert(certPath, certPwd)
23
+ def self.initSignCert(certPath, certPwd)
30
24
  if !certPath || !certPwd
31
- LogUtil.info("signCertPath or signCertPwd is none, exit initSignCert")
25
+ LogUtil.info('signCertPath or signCertPwd is none, exit initSignCert')
32
26
  return
33
27
  end
34
- LogUtil.info("读取签名证书……")
28
+ LogUtil.info('读取签名证书……')
35
29
  cert = Cert.new
36
30
  file = IO.binread(certPath)
37
31
  p12 = OpenSSL::PKCS12.new(file, certPwd)
@@ -39,89 +33,76 @@ module UnionPei
39
33
  cert.cert = p12.certificate
40
34
  cert.key = p12.key
41
35
  @@signCerts[certPath] = cert
42
- LogUtil.info("签名证书读取成功,序列号:" + cert.certId)
43
- end
44
-
45
- def CertUtil.initEncryptCert(certPath=SDKConfig.instance.encryptCertPath)
46
- if !certPath
47
- LogUtil.info("encryptCertPath is none, exit initEncryptCert")
48
- return
49
- end
50
- LogUtil.info("读取加密证书……")
51
- cert = Cert.new
52
- file = IO.binread(certPath)
53
- x509Cert = OpenSSL::X509::Certificate.new(file)
54
- cert.cert = x509Cert
55
- cert.certId = x509Cert.serial.to_s
56
- cert.key = x509Cert.public_key
57
- @@encryptCert[certPath] = cert
58
- LogUtil.info("加密证书读取成功,序列号:" + cert.certId)
36
+ LogUtil.info("签名证书读取成功,序列号:#{cert.certId}")
59
37
  end
60
38
 
61
- def CertUtil.initRootCert()
62
- if @@rootCert
39
+ def self.initEncryptCert(certPath = SDKConfig.instance.encryptCertPath)
40
+ unless certPath
41
+ LogUtil.info('encryptCertPath is none, exit initEncryptCert')
63
42
  return
64
43
  end
65
- if !SDKConfig.instance.rootCertPath
66
- LogUtil.info("rootCertPath is none, exit initRootCert")
44
+ LogUtil.info('读取加密证书……')
45
+ cert = Cert.new
46
+ file = IO.binread(certPath)
47
+ x509Cert = OpenSSL::X509::Certificate.new(file)
48
+ cert.cert = x509Cert
49
+ cert.certId = x509Cert.serial.to_s
50
+ cert.key = x509Cert.public_key
51
+ @@encryptCert[certPath] = cert
52
+ LogUtil.info("加密证书读取成功,序列号:#{cert.certId}")
53
+ end
54
+
55
+ def self.initRootCert
56
+ return if @@rootCert
57
+
58
+ unless SDKConfig.instance.rootCertPath
59
+ LogUtil.info('rootCertPath is none, exit initRootCert')
67
60
  return
68
61
  end
69
- LogUtil.info("start initRootCert")
62
+ LogUtil.info('start initRootCert')
70
63
  file = IO.binread(SDKConfig.instance.rootCertPath)
71
64
  x509Cert = OpenSSL::X509::Certificate.new(file)
72
65
  @@rootCert = x509Cert
73
- LogUtil.info("initRootCert succeed")
66
+ LogUtil.info('initRootCert succeed')
74
67
  end
75
68
 
76
- def CertUtil.initMiddleCert()
77
- if @@middleCert
78
- return
79
- end
80
- if !SDKConfig.instance.middleCertPath
81
- LogUtil.info("middleCertPath is none, exit initMiddleCert")
69
+ def self.initMiddleCert
70
+ return if @@middleCert
71
+
72
+ unless SDKConfig.instance.middleCertPath
73
+ LogUtil.info('middleCertPath is none, exit initMiddleCert')
82
74
  return
83
75
  end
84
- LogUtil.info("start initMiddleCert")
76
+ LogUtil.info('start initMiddleCert')
85
77
  file = IO.binread(SDKConfig.instance.middleCertPath)
86
78
  x509Cert = OpenSSL::X509::Certificate.new(file)
87
79
  @@middleCert = x509Cert
88
- LogUtil.info("initMiddleCert succeed")
80
+ LogUtil.info('initMiddleCert succeed')
89
81
  end
90
82
 
91
- public
92
- def CertUtil.getSignPriKey(certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
93
- if !@@signCerts[certPath]
94
- CertUtil.initSignCert(certPath, certPwd)
95
- end
83
+ def self.getSignPriKey(certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
84
+ CertUtil.initSignCert(certPath, certPwd) unless @@signCerts[certPath]
96
85
  @@signCerts[certPath].key
97
86
  end
98
87
 
99
- def CertUtil.getSignCertId(certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
100
- if !@@signCerts[certPath]
101
- CertUtil.initSignCert(certPath, certPwd)
102
- end
88
+ def self.getSignCertId(certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
89
+ CertUtil.initSignCert(certPath, certPwd) unless @@signCerts[certPath]
103
90
  @@signCerts[certPath].certId
104
91
  end
105
92
 
106
- def CertUtil.getEncryptKey(certPath=SDKConfig.instance.encryptCertPath)
107
- if !@@encryptCert[certPath]
108
- CertUtil.initEncryptCert(certPath)
109
- end
93
+ def self.getEncryptKey(certPath = SDKConfig.instance.encryptCertPath)
94
+ CertUtil.initEncryptCert(certPath) unless @@encryptCert[certPath]
110
95
  @@encryptCert[certPath].key
111
96
  end
112
97
 
113
- def CertUtil.getEncryptCertId(certPath=SDKConfig.instance.encryptCertPath)
114
- if !@@encryptCert[certPath]
115
- CertUtil.initEncryptCert(certPath)
116
- end
98
+ def self.getEncryptCertId(certPath = SDKConfig.instance.encryptCertPath)
99
+ CertUtil.initEncryptCert(certPath) unless @@encryptCert[certPath]
117
100
  @@encryptCert[certPath].certId
118
101
  end
119
102
 
120
- def CertUtil.verifyAndGetVerifyKey(certBase64String)
103
+ def self.verifyAndGetVerifyKey(certBase64String)
104
+ return @@verifyCerts5_1_0[certBase64String].key if @@verifyCerts5_1_0[certBase64String]
121
105
 
122
- if @@verifyCerts5_1_0[certBase64String]
123
- return @@verifyCerts5_1_0[certBase64String].key
124
- end
125
106
  initMiddleCert
126
107
  initRootCert
127
108
 
@@ -137,31 +118,31 @@ module UnionPei
137
118
  store.add_cert(x509Cert)
138
119
  store.add_cert(@@middleCert)
139
120
  store.add_cert(@@rootCert)
140
- if !store.verify(x509Cert)
141
- LogUtil.error("validate signPubKeyCert by cert chain failed, error=" + store.error + ", error string=" + store.error_string)
121
+ unless store.verify(x509Cert)
122
+ LogUtil.error("validate signPubKeyCert by cert chain failed, error=#{store.error}, error string=#{store.error_string}")
142
123
  return nil
143
124
  end
144
125
 
145
126
  sSubject = x509Cert.subject.to_s
146
- ss = sSubject.split("@")
127
+ ss = sSubject.split('@')
147
128
  if ss.length <= 2
148
- LogUtil.error("error sSubject: " + sSubject)
129
+ LogUtil.error("error sSubject: #{sSubject}")
149
130
  return nil
150
131
  end
151
- cn = ss[2];
132
+ cn = ss[2]
152
133
  if SDKConfig.instance.ifValidateCNName
153
134
  if UNIONPAY_CNNAME != cn
154
- LogUtil.error("cer owner is not CUP:" + cn)
135
+ LogUtil.error("cer owner is not CUP:#{cn}")
155
136
  return nil
156
- elsif UNIONPAY_CNNAME != cn and cn != "00040000:SIGN" #测试环境目前是00040000:SIGN
157
- LogUtil.error("cer owner is not CUP:" + cn)
137
+ elsif (UNIONPAY_CNNAME != cn) && (cn != '00040000:SIGN') # 测试环境目前是00040000:SIGN
138
+ LogUtil.error("cer owner is not CUP:#{cn}")
158
139
  return nil
159
140
  end
160
141
  end
161
142
 
162
- LogUtil.info("validate signPubKeyCert by cert succeed: " + certBase64String)
163
- @@verifyCerts5_1_0[certBase64String] = cert;
164
- return @@verifyCerts5_1_0[certBase64String].key
143
+ LogUtil.info("validate signPubKeyCert by cert succeed: #{certBase64String}")
144
+ @@verifyCerts5_1_0[certBase64String] = cert
145
+ @@verifyCerts5_1_0[certBase64String].key
165
146
 
166
147
  # 用bc的jar用中级证书验证可以单独验时间,然后再用中级证书验一下,但为了和谐统一,目前改store验证书链验证了。
167
148
  # if Time.new<x509Cert.not_before or Time.new>x509Cert.not_after
@@ -176,19 +157,17 @@ module UnionPei
176
157
  # end
177
158
  end
178
159
 
179
- def CertUtil.getDecryptPriKey(certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
180
- if !@@signCerts[certPath]
181
- CertUtil.initSignCert(certPath, certPwd)
182
- end
160
+ def self.getDecryptPriKey(certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
161
+ CertUtil.initSignCert(certPath, certPwd) unless @@signCerts[certPath]
183
162
  @@signCerts[certPath].key
184
163
  end
185
164
 
186
- def CertUtil.resetEncryptCertPublicKey()
165
+ def self.resetEncryptCertPublicKey
187
166
  @@encryptCert = {}
188
167
  CertUtil.initEncryptCert
189
168
  end
190
169
 
191
- def CertUtil.getX509Cert(strCert)
170
+ def self.getX509Cert(strCert)
192
171
  OpenSSL::X509::Certificate.new(strCert)
193
172
  end
194
173
  end
@@ -1,4 +1,4 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'singleton'
4
4
  require 'logger'
@@ -8,75 +8,68 @@ require_relative 'sdk_config'
8
8
 
9
9
  module UnionPei
10
10
  class LogUtil
11
-
12
11
  @@logger = nil
13
12
 
14
13
  private_class_method :new
15
14
 
16
- private
17
-
18
- def LogUtil.getLogger
19
- if !@@logger
20
- puts "init LogUtil"
21
- if SDKConfig.instance.logFilePath.nil?
22
- @@logger = Logger.new(STDOUT)
23
- else
24
- @@logger = Logger.new(SDKConfig.instance.logFilePath)
25
- end
15
+ def self.getLogger
16
+ unless @@logger
17
+ @@logger = if SDKConfig.instance.logFilePath.nil?
18
+ Logger.new($stdout)
19
+ else
20
+ Logger.new(SDKConfig.instance.logFilePath)
21
+ end
26
22
  @@logger.datetime_format = '%Y-%m-%d %H:%M:%S'
27
23
  @@logger.formatter = proc do |severity, datetime, progname, msg|
28
24
  "#{datetime} [#{severity}] #{progname}: #{msg}\n"
29
25
  end
30
26
  @@logger.level = case SDKConfig.instance.logLevel.upcase
31
- when 'INFO' then
27
+ when 'INFO'
32
28
  Logger::INFO
33
- when 'DEBUG' then
29
+ when 'DEBUG'
34
30
  Logger::DEBUG
35
- when 'WARN' then
31
+ when 'WARN'
36
32
  Logger::WARN
37
- when 'ERROR' then
33
+ when 'ERROR'
38
34
  Logger::ERROR
39
- when 'FATAL' then
35
+ when 'FATAL'
40
36
  Logger::FATAL
41
37
  else
42
38
  Logger::UNKNOWN
43
- end
39
+ end
44
40
  end
45
- p = LogUtil.parse_caller(caller(0)[2]) #可能有多线程问题?我才不管哼
46
- @@logger.progname = p[0].to_s + ":" + p[1].to_s
41
+ p = LogUtil.parse_caller(caller(0)[2])
42
+ @@logger.progname = "#{p[0]}:#{p[1]}"
47
43
  @@logger
48
44
  end
49
45
 
50
- def LogUtil.parse_caller(at)
46
+ def self.parse_caller(at)
51
47
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
52
- file = $1
53
- line = $2.to_i
54
- method = $3
48
+ file = Regexp.last_match(1)
49
+ line = Regexp.last_match(2).to_i
50
+ method = Regexp.last_match(3)
55
51
  [file, line, method]
56
52
  end
57
53
  end
58
54
 
59
- public
60
-
61
- def LogUtil.info(msg)
55
+ def self.info(msg)
62
56
  LogUtil.getLogger.info(msg)
63
57
  end
64
58
 
65
- def LogUtil.debug(msg)
59
+ def self.debug(msg)
66
60
  LogUtil.getLogger.debug(msg)
67
61
  end
68
62
 
69
- def LogUtil.warn(msg)
63
+ def self.warn(msg)
70
64
  LogUtil.getLogger.warn(msg)
71
65
  end
72
66
 
73
- def LogUtil.error(msg)
67
+ def self.error(msg)
74
68
  LogUtil.getLogger.error(msg)
75
69
  end
76
70
 
77
- def LogUtil.fatal(msg)
71
+ def self.fatal(msg)
78
72
  LogUtil.getLogger.fatal(msg)
79
73
  end
80
-
81
74
  end
82
75
  end