netpayclient 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 4f2df075a9598db18b01be42d8b2ef3ae9a68fed
4
- data.tar.gz: 89cb2d099447a49fae3a52ea79fd115078d08ddd
3
+ metadata.gz: c108bf07606362dd67e5b4df2a5021bb66cf8ccc
4
+ data.tar.gz: fc735cf824e9574dafde7f4e9ab376b7405da478
5
5
  SHA512:
6
- metadata.gz: 3587ef07030a6df4691e2a95d03b6f8533ffdc44db8a53e22cac0b56cf01b689be1e3baa87d6da920623bb31a05dbdfd695f004c6b0c72e770be02f70806655e
7
- data.tar.gz: 56cd104a3d9d656bc2fcc753f1acdd104e3b3b7e474971822852a54310032708514a56861076006f075e27d18056b59354651aa0f4d3a66584c44ec0ec4a5181
6
+ metadata.gz: d8ae55663f820204ee40cb1f134276e9dd15fc6973a0bcc0ec03dfa2a6487693f12324ad12fd439cced8122bc5432c78bea15d51fc2dc65de32cd80b2d89483b
7
+ data.tar.gz: d804094e48e532d0cfe8c4d343abeb0e599523223a87d7c970db33981beb40bf68a6336841ef7b080715cd9b03650f5aecd2242d835ea3b09de72fe390120b64
data/README.md CHANGED
@@ -9,23 +9,36 @@
9
9
  sudo apt-get install mcrypt libmcrypt-dev
10
10
  ```
11
11
 
12
- ## API
12
+ ## 使用 & API
13
13
 
14
14
  ### Netpayclient.build_key(path: nil, hash: {})
15
15
  ```ruby
16
- Netpayclient.build_key(path: 'path/to/MerPrK.key')
16
+ netpay = Netpayclient.build_key(path: 'path/to/MerPrK.key')
17
17
  # or
18
- Netpayclient.build_key(hash: {
18
+ netpay = Netpayclient.build_key(hash: {
19
19
  MERID: '8435...4395',
20
20
  prikeyS: '4234...423',
21
21
  prikeyE: '472...48324',
22
22
  })
23
23
  ```
24
24
 
25
- ### Netpayclient.sign(msg)
25
+ ### 签名 sign
26
+ ```ruby
27
+ # netpay = Netpayclient.build_key(...)
28
+ netpay.sign('834...645')
29
+ ```
26
30
 
27
- ### Netpayclient.sign_order(merid, ordno, amount, curyid, transdate, transtype)
31
+ ### 订单签名 sign_order
32
+ ```ruby
33
+ netpay.sign_order(merid, ordno, amount, curyid, transdate, transtype)
34
+ ```
28
35
 
29
- ### Netpayclient.verify(plain, checkvalue)
36
+ ### 应答数据的签名验证 verify
37
+ ```ruby
38
+ netpay.verify(plain, checkvalue)
39
+ ```
30
40
 
31
- ### Netpayclient.verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
41
+ ### 应答数据的签名包装 verify_trans_response
42
+ ```ruby
43
+ netpay.verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
44
+ ```
@@ -1,3 +1,3 @@
1
1
  module Netpayclient
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/netpayclient.rb CHANGED
@@ -1,15 +1,31 @@
1
1
  require "netpayclient/version"
2
2
 
3
- module Netpayclient
4
- require 'digest/sha1'
5
- require 'mcrypt'
6
- require 'iniparse'
3
+ class Netpayclient
7
4
  require 'openssl'
8
5
 
9
6
  DES_KEY = 'SCUBEPGW'
10
7
  HASH_PAD = '0001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff003021300906052b0e03021a05000414'
11
8
 
12
- @@private_key = {}
9
+ module Crypto
10
+ require 'mcrypt'
11
+ def self.crypto
12
+ if defined?(@@crypto).nil?
13
+ @@crypto = Mcrypt.new(:des, :cbc)
14
+ @@crypto.key = DES_KEY
15
+ @@crypto.iv = "\x00" * 8
16
+ @@crypto.padding = false
17
+ end
18
+ @@crypto
19
+ end
20
+
21
+ def self.decrypt(str)
22
+ if str.blank?
23
+ "\xEE\xB3\x16\x86\xAB\x84G\x90"
24
+ else
25
+ self.crypto.decrypt(str)
26
+ end
27
+ end
28
+ end
13
29
 
14
30
  def self.hex2bin(hexdata)
15
31
  [hexdata].pack "H*"
@@ -38,6 +54,7 @@ module Netpayclient
38
54
  end
39
55
 
40
56
  def self.sha1_128(string)
57
+ require 'digest/sha1'
41
58
  hash = Digest::SHA1.hexdigest(string)
42
59
  sha_bin = self.hex2bin(hash)
43
60
  sha_pad = self.hex2bin(HASH_PAD)
@@ -75,18 +92,13 @@ module Netpayclient
75
92
  ret.size == 256 ? ret : false
76
93
  end
77
94
 
78
- def self.rsa_decrypt(input)
79
- check = self.bchexdec(input)
80
- modulus = self.bin2int(@@private_key["modulus"])
81
- exponent = self.bchexdec("010001")
82
- result = self.mybcpowmod(check,exponent,modulus)
83
- rb = self.bcdechex(result)
84
- self.padstr(rb).upcase
95
+ def self.build_key(path: nil, hash: {})
96
+ self.new(path: path, hash: hash)
85
97
  end
86
98
 
87
- def self.build_key(path: nil, hash: {})
88
- @@private_key.clear
89
- ret = false
99
+ def initialize(path: nil, hash: {})
100
+ require 'iniparse'
101
+ @private_key = {}
90
102
  if path
91
103
  config_hash = IniParse.parse(File.read(path))['NetPayClient']
92
104
  else
@@ -95,51 +107,53 @@ module Netpayclient
95
107
  hex = ""
96
108
  if not config_hash['MERID'].nil?
97
109
  ret = config_hash['MERID']
98
- @@private_key[:MERID] = ret
110
+ @private_key[:MERID] = ret
99
111
  hex = config_hash['prikeyS'][80...config_hash['prikeyS'].size]
100
112
  elsif not config_hash['PGID'].nil?
101
113
  ret = config_hash['PGID']
102
- @@private_key[:PGID] = ret
114
+ @private_key[:PGID] = ret
103
115
  hex = config_hash['pubkeyS'][48...config_hash['pubkeyS'].size]
104
116
  else
105
- return ret
117
+ raise 'config error'
106
118
  end
107
- bin = self.hex2bin(hex)
108
- @@private_key[:modulus] = bin[0,128]
109
-
110
- iv = "\x00" * 8
111
- crypto = Mcrypt.new(:des, :cbc)
112
- crypto.key = DES_KEY
113
- crypto.iv = iv
114
- crypto.padding = false
119
+ bin = self.class.hex2bin(hex)
120
+ @private_key[:modulus] = bin[0,128]
115
121
 
116
122
  prime1 = bin[384,64]
117
- enc = crypto.decrypt(prime1)
118
- @@private_key[:prime1] = enc
123
+ enc = Crypto.decrypt(prime1)
124
+ @private_key[:prime1] = enc
119
125
  prime2 = bin[448,64]
120
- enc = crypto.decrypt(prime2)
121
- @@private_key[:prime2] = enc
126
+ enc = Crypto.decrypt(prime2)
127
+ @private_key[:prime2] = enc
122
128
  prime_exponent1 = bin[512,64]
123
- enc = crypto.decrypt(prime_exponent1)
124
- @@private_key[:prime_exponent1] = enc
129
+ enc = Crypto.decrypt(prime_exponent1)
130
+ @private_key[:prime_exponent1] = enc
125
131
  prime_exponent2 = bin[576,64]
126
- enc = crypto.decrypt(prime_exponent2)
127
- @@private_key[:prime_exponent2] = enc
132
+ enc = Crypto.decrypt(prime_exponent2)
133
+ @private_key[:prime_exponent2] = enc
128
134
  coefficient = bin[640,64]
129
- enc = crypto.decrypt(coefficient)
130
- @@private_key[:coefficient] = enc
131
- return ret
135
+ enc = Crypto.decrypt(coefficient)
136
+ @private_key[:coefficient] = enc
137
+ end
138
+
139
+ def rsa_decrypt(input)
140
+ check = self.class.bchexdec(input)
141
+ modulus = self.class.bin2int(@private_key[:modulus])
142
+ exponent = self.class.bchexdec("010001")
143
+ result = self.class.mybcpowmod(check, exponent, modulus)
144
+ rb = self.class.bcdechex(result)
145
+ self.class.padstr(rb).upcase
132
146
  end
133
147
 
134
- def self.sign(msg)
135
- if not @@private_key.key?(:MERID)
148
+ def sign(msg)
149
+ if not @private_key.key?(:MERID)
136
150
  return false
137
151
  end
138
- hb = self.sha1_128(msg)
139
- return self.rsa_encrypt(@@private_key, hb)
152
+ hb = self.class.sha1_128(msg)
153
+ return self.class.rsa_encrypt(@private_key, hb)
140
154
  end
141
155
 
142
- def self.sign_order(merid, ordno, amount, curyid, transdate, transtype)
156
+ def sign_order(merid, ordno, amount, curyid, transdate, transtype)
143
157
  return false if (merid.size!=15)
144
158
  return false if (ordno.size!=16)
145
159
  return false if (amount.size!=12)
@@ -147,19 +161,19 @@ module Netpayclient
147
161
  return false if (transdate.size!=8)
148
162
  return false if (transtype.size!=4)
149
163
  plain = merid + ordno + amount + curyid + transdate + transtype
150
- return self.sign(plain)
164
+ return sign(plain)
151
165
  end
152
166
 
153
- def self.verify(plain, check)
154
- return false if not @@private_key.key?(:PGID)
167
+ def verify(plain, check)
168
+ return false if not @private_key.key?(:PGID)
155
169
  return false if check.size != 256
156
- hb = self.sha1_128(plain)
170
+ hb = self.class.sha1_128(plain)
157
171
  hbhex = hb.unpack('H*')[0].upcase
158
- rbhex = self.rsa_decrypt(check)
172
+ rbhex = rsa_decrypt(check)
159
173
  return hbhex == rbhex ? true : false
160
174
  end
161
175
 
162
- def self.verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
176
+ def verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
163
177
  return false if (merid.size!=15)
164
178
  return false if (ordno.size!=16)
165
179
  return false if (amount.size!=12)
@@ -169,6 +183,6 @@ module Netpayclient
169
183
  return false if (ordstatus.size!=4)
170
184
  return false if (check.size!=256)
171
185
  plain = merid + ordno + amount + curyid + transdate + transtype + ordstatus
172
- return self.verify(plain, check)
186
+ return verify(plain, check)
173
187
  end
174
188
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netpayclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weirong Xu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-26 00:00:00.000000000 Z
11
+ date: 2016-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: iniparse