netpayclient 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c108bf07606362dd67e5b4df2a5021bb66cf8ccc
4
- data.tar.gz: fc735cf824e9574dafde7f4e9ab376b7405da478
3
+ metadata.gz: 32c3909cc8ec5db2b3009059574bc91c6733602e
4
+ data.tar.gz: c33527542332eed2a9983ff5fb84ddf74f50062b
5
5
  SHA512:
6
- metadata.gz: d8ae55663f820204ee40cb1f134276e9dd15fc6973a0bcc0ec03dfa2a6487693f12324ad12fd439cced8122bc5432c78bea15d51fc2dc65de32cd80b2d89483b
7
- data.tar.gz: d804094e48e532d0cfe8c4d343abeb0e599523223a87d7c970db33981beb40bf68a6336841ef7b080715cd9b03650f5aecd2242d835ea3b09de72fe390120b64
6
+ metadata.gz: 352035e63a04e75ac656c266eeeb7c3053966ff9a20ae5953c36955934da43b0887a11ae3f6e3a1347ae50ffad8d55861e91adc71cbfe8d0a5e4edad4767b2f3
7
+ data.tar.gz: d9778fc97ca42a68aa7f61c1f710005f18d1af8a28a1b631b4da3d811c93625b5dbc106073fd3a7bb055691165ba532fda39d9b0cb4f1ea7c975cb0522d9d359
data/README.md CHANGED
@@ -38,7 +38,7 @@ netpay.sign_order(merid, ordno, amount, curyid, transdate, transtype)
38
38
  netpay.verify(plain, checkvalue)
39
39
  ```
40
40
 
41
- ### 应答数据的签名包装 verify_trans_response
41
+ ### 应答数据的签名验证包装 verify_trans_response
42
42
  ```ruby
43
43
  netpay.verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
44
44
  ```
@@ -1,3 +1,3 @@
1
1
  module Netpayclient
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/netpayclient.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "netpayclient/version"
2
2
 
3
- class Netpayclient
3
+ module Netpayclient
4
4
  require 'openssl'
5
5
 
6
6
  DES_KEY = 'SCUBEPGW'
@@ -27,11 +27,15 @@ class Netpayclient
27
27
  end
28
28
  end
29
29
 
30
- def self.hex2bin(hexdata)
30
+ def self.build_key(path: nil, hash: {})
31
+ Netpayclient.new(path: path, hash: hash)
32
+ end
33
+
34
+ def hex2bin(hexdata)
31
35
  [hexdata].pack "H*"
32
36
  end
33
37
 
34
- def self.padstr(src, len=256, chr='0', d='L')
38
+ def padstr(src, len=256, chr='0', d='L')
35
39
  src.strip!
36
40
  case d
37
41
  when 'L'
@@ -41,41 +45,41 @@ class Netpayclient
41
45
  end
42
46
  end
43
47
 
44
- def self.bin2int(bindata)
45
- self.bchexdec(bindata.unpack('H*')[0])
48
+ def bin2int(bindata)
49
+ bchexdec(bindata.unpack('H*')[0])
46
50
  end
47
51
 
48
- def self.bchexdec(hexdata)
52
+ def bchexdec(hexdata)
49
53
  hexdata.to_i(16)
50
54
  end
51
55
 
52
- def self.bcdechex(decdata)
56
+ def bcdechex(decdata)
53
57
  decdata.to_s(16)
54
58
  end
55
59
 
56
- def self.sha1_128(string)
60
+ def sha1_128(string)
57
61
  require 'digest/sha1'
58
62
  hash = Digest::SHA1.hexdigest(string)
59
- sha_bin = self.hex2bin(hash)
60
- sha_pad = self.hex2bin(HASH_PAD)
63
+ sha_bin = hex2bin(hash)
64
+ sha_pad = hex2bin(HASH_PAD)
61
65
  sha_pad + sha_bin
62
66
  end
63
67
 
64
- def self.mybcpowmod(num, pow, mod)
68
+ def mybcpowmod(num, pow, mod)
65
69
  num.to_bn.mod_exp(pow, mod)
66
70
  end
67
71
 
68
- def self.rsa_encrypt(private_key, input)
69
- p = self.bin2int(private_key[:prime1])
70
- q = self.bin2int(private_key[:prime2])
71
- u = self.bin2int(private_key[:coefficient])
72
- dP = self.bin2int(private_key[:prime_exponent1])
73
- dQ = self.bin2int(private_key[:prime_exponent2])
74
- c = self.bin2int(input)
72
+ def rsa_encrypt(private_key, input)
73
+ p = bin2int(private_key[:prime1])
74
+ q = bin2int(private_key[:prime2])
75
+ u = bin2int(private_key[:coefficient])
76
+ dP = bin2int(private_key[:prime_exponent1])
77
+ dQ = bin2int(private_key[:prime_exponent2])
78
+ c = bin2int(input)
75
79
  cp = c % p
76
80
  cq = c % q
77
- a = self.mybcpowmod(cp, dP, p)
78
- b = self.mybcpowmod(cq, dQ, q)
81
+ a = mybcpowmod(cp, dP, p)
82
+ b = mybcpowmod(cq, dQ, q)
79
83
  if a > b
80
84
  result = a - b
81
85
  else
@@ -87,102 +91,100 @@ class Netpayclient
87
91
  result = result % p
88
92
  result = result * q
89
93
  result = result + b
90
- ret = self.bcdechex(result)
91
- ret = self.padstr(ret).upcase
94
+ ret = bcdechex(result)
95
+ ret = padstr(ret).upcase
92
96
  ret.size == 256 ? ret : false
93
97
  end
94
98
 
95
- def self.build_key(path: nil, hash: {})
96
- self.new(path: path, hash: hash)
97
- end
98
-
99
- def initialize(path: nil, hash: {})
100
- require 'iniparse'
101
- @private_key = {}
102
- if path
103
- config_hash = IniParse.parse(File.read(path))['NetPayClient']
104
- else
105
- config_hash = hash
106
- end
107
- hex = ""
108
- if not config_hash['MERID'].nil?
109
- ret = config_hash['MERID']
110
- @private_key[:MERID] = ret
111
- hex = config_hash['prikeyS'][80...config_hash['prikeyS'].size]
112
- elsif not config_hash['PGID'].nil?
113
- ret = config_hash['PGID']
114
- @private_key[:PGID] = ret
115
- hex = config_hash['pubkeyS'][48...config_hash['pubkeyS'].size]
116
- else
117
- raise 'config error'
99
+ class Netpayclient
100
+ def initialize(path: nil, hash: {})
101
+ require 'iniparse'
102
+ @private_key = {}
103
+ if path
104
+ config_hash = IniParse.parse(File.read(path))['NetPayClient']
105
+ else
106
+ config_hash = hash
107
+ end
108
+ hex = ""
109
+ if not config_hash['MERID'].nil?
110
+ ret = config_hash['MERID']
111
+ @private_key[:MERID] = ret
112
+ hex = config_hash['prikeyS'][80...config_hash['prikeyS'].size]
113
+ elsif not config_hash['PGID'].nil?
114
+ ret = config_hash['PGID']
115
+ @private_key[:PGID] = ret
116
+ hex = config_hash['pubkeyS'][48...config_hash['pubkeyS'].size]
117
+ else
118
+ raise 'config error'
119
+ end
120
+ bin = hex2bin(hex)
121
+ @private_key[:modulus] = bin[0,128]
122
+
123
+ prime1 = bin[384,64]
124
+ enc = Crypto.decrypt(prime1)
125
+ @private_key[:prime1] = enc
126
+ prime2 = bin[448,64]
127
+ enc = Crypto.decrypt(prime2)
128
+ @private_key[:prime2] = enc
129
+ prime_exponent1 = bin[512,64]
130
+ enc = Crypto.decrypt(prime_exponent1)
131
+ @private_key[:prime_exponent1] = enc
132
+ prime_exponent2 = bin[576,64]
133
+ enc = Crypto.decrypt(prime_exponent2)
134
+ @private_key[:prime_exponent2] = enc
135
+ coefficient = bin[640,64]
136
+ enc = Crypto.decrypt(coefficient)
137
+ @private_key[:coefficient] = enc
118
138
  end
119
- bin = self.class.hex2bin(hex)
120
- @private_key[:modulus] = bin[0,128]
121
-
122
- prime1 = bin[384,64]
123
- enc = Crypto.decrypt(prime1)
124
- @private_key[:prime1] = enc
125
- prime2 = bin[448,64]
126
- enc = Crypto.decrypt(prime2)
127
- @private_key[:prime2] = enc
128
- prime_exponent1 = bin[512,64]
129
- enc = Crypto.decrypt(prime_exponent1)
130
- @private_key[:prime_exponent1] = enc
131
- prime_exponent2 = bin[576,64]
132
- enc = Crypto.decrypt(prime_exponent2)
133
- @private_key[:prime_exponent2] = enc
134
- coefficient = bin[640,64]
135
- enc = Crypto.decrypt(coefficient)
136
- @private_key[:coefficient] = enc
137
- end
138
139
 
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
146
- end
140
+ def rsa_decrypt(input)
141
+ check = bchexdec(input)
142
+ modulus = bin2int(@private_key[:modulus])
143
+ exponent = bchexdec("010001")
144
+ result = mybcpowmod(check, exponent, modulus)
145
+ rb = bcdechex(result)
146
+ padstr(rb).upcase
147
+ end
147
148
 
148
- def sign(msg)
149
- if not @private_key.key?(:MERID)
150
- return false
149
+ def sign(msg)
150
+ if not @private_key.key?(:MERID)
151
+ return false
152
+ end
153
+ hb = sha1_128(msg)
154
+ return rsa_encrypt(@private_key, hb)
151
155
  end
152
- hb = self.class.sha1_128(msg)
153
- return self.class.rsa_encrypt(@private_key, hb)
154
- end
155
156
 
156
- def sign_order(merid, ordno, amount, curyid, transdate, transtype)
157
- return false if (merid.size!=15)
158
- return false if (ordno.size!=16)
159
- return false if (amount.size!=12)
160
- return false if (curyid.size!=3)
161
- return false if (transdate.size!=8)
162
- return false if (transtype.size!=4)
163
- plain = merid + ordno + amount + curyid + transdate + transtype
164
- return sign(plain)
165
- end
157
+ def sign_order(merid, ordno, amount, curyid, transdate, transtype)
158
+ return false if (merid.size!=15)
159
+ return false if (ordno.size!=16)
160
+ return false if (amount.size!=12)
161
+ return false if (curyid.size!=3)
162
+ return false if (transdate.size!=8)
163
+ return false if (transtype.size!=4)
164
+ plain = merid + ordno + amount + curyid + transdate + transtype
165
+ return sign(plain)
166
+ end
166
167
 
167
- def verify(plain, check)
168
- return false if not @private_key.key?(:PGID)
169
- return false if check.size != 256
170
- hb = self.class.sha1_128(plain)
171
- hbhex = hb.unpack('H*')[0].upcase
172
- rbhex = rsa_decrypt(check)
173
- return hbhex == rbhex ? true : false
174
- end
168
+ def verify(plain, check)
169
+ return false if not @private_key.key?(:PGID)
170
+ return false if check.size != 256
171
+ hb = sha1_128(plain)
172
+ hbhex = hb.unpack('H*')[0].upcase
173
+ rbhex = rsa_decrypt(check)
174
+ return hbhex == rbhex ? true : false
175
+ end
175
176
 
176
- def verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
177
- return false if (merid.size!=15)
178
- return false if (ordno.size!=16)
179
- return false if (amount.size!=12)
180
- return false if (curyid.size!=3)
181
- return false if (transdate.size!=8)
182
- return false if (transtype.size!=4)
183
- return false if (ordstatus.size!=4)
184
- return false if (check.size!=256)
185
- plain = merid + ordno + amount + curyid + transdate + transtype + ordstatus
186
- return verify(plain, check)
177
+ def verify_trans_response(merid, ordno, amount, curyid, transdate, transtype, ordstatus, check)
178
+ return false if (merid.size!=15)
179
+ return false if (ordno.size!=16)
180
+ return false if (amount.size!=12)
181
+ return false if (curyid.size!=3)
182
+ return false if (transdate.size!=8)
183
+ return false if (transtype.size!=4)
184
+ return false if (ordstatus.size!=4)
185
+ return false if (check.size!=256)
186
+ plain = merid + ordno + amount + curyid + transdate + transtype + ordstatus
187
+ return verify(plain, check)
188
+ end
187
189
  end
188
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netpayclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weirong Xu