netpayclient 0.1.2 → 0.1.3

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: 32c3909cc8ec5db2b3009059574bc91c6733602e
4
- data.tar.gz: c33527542332eed2a9983ff5fb84ddf74f50062b
3
+ metadata.gz: 62212890bf3ca078a71659b706570f0a9dec45e3
4
+ data.tar.gz: 3ce244d5ccdd7977bc347de5986ecf1e149e7659
5
5
  SHA512:
6
- metadata.gz: 352035e63a04e75ac656c266eeeb7c3053966ff9a20ae5953c36955934da43b0887a11ae3f6e3a1347ae50ffad8d55861e91adc71cbfe8d0a5e4edad4767b2f3
7
- data.tar.gz: d9778fc97ca42a68aa7f61c1f710005f18d1af8a28a1b631b4da3d811c93625b5dbc106073fd3a7bb055691165ba532fda39d9b0cb4f1ea7c975cb0522d9d359
6
+ metadata.gz: f5d44ab20e3fb4fae49ac7c8ea29c310b2792347f0543d87ca169cb23418e685f09eae604fe4b98a78aa084c237c84d50f5b229a0058a6e9ea9b42c7fbf7ce9c
7
+ data.tar.gz: d14ee1ca5d0fd7d490a762ec5ed30c7c342525507032619d145a81364667f6506dc810a496ccc6694b6c796710d13e0660d719fb8fea601468363718c7496d37
@@ -1,3 +1,3 @@
1
1
  module Netpayclient
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/netpayclient.rb CHANGED
@@ -31,72 +31,74 @@ module Netpayclient
31
31
  Netpayclient.new(path: path, hash: hash)
32
32
  end
33
33
 
34
- def hex2bin(hexdata)
35
- [hexdata].pack "H*"
36
- end
34
+ class Netpayclient
35
+ class << self
36
+ def hex2bin(hexdata)
37
+ [hexdata].pack "H*"
38
+ end
37
39
 
38
- def padstr(src, len=256, chr='0', d='L')
39
- src.strip!
40
- case d
41
- when 'L'
42
- src.rjust(len, chr)
43
- else
44
- src.ljust(len, chr)
45
- end
46
- end
40
+ def padstr(src, len=256, chr='0', d='L')
41
+ src.strip!
42
+ case d
43
+ when 'L'
44
+ src.rjust(len, chr)
45
+ else
46
+ src.ljust(len, chr)
47
+ end
48
+ end
47
49
 
48
- def bin2int(bindata)
49
- bchexdec(bindata.unpack('H*')[0])
50
- end
50
+ def bin2int(bindata)
51
+ bchexdec(bindata.unpack('H*')[0])
52
+ end
51
53
 
52
- def bchexdec(hexdata)
53
- hexdata.to_i(16)
54
- end
54
+ def bchexdec(hexdata)
55
+ hexdata.to_i(16)
56
+ end
55
57
 
56
- def bcdechex(decdata)
57
- decdata.to_s(16)
58
- end
58
+ def bcdechex(decdata)
59
+ decdata.to_s(16)
60
+ end
59
61
 
60
- def sha1_128(string)
61
- require 'digest/sha1'
62
- hash = Digest::SHA1.hexdigest(string)
63
- sha_bin = hex2bin(hash)
64
- sha_pad = hex2bin(HASH_PAD)
65
- sha_pad + sha_bin
66
- end
62
+ def sha1_128(string)
63
+ require 'digest/sha1'
64
+ hash = Digest::SHA1.hexdigest(string)
65
+ sha_bin = hex2bin(hash)
66
+ sha_pad = hex2bin(HASH_PAD)
67
+ sha_pad + sha_bin
68
+ end
67
69
 
68
- def mybcpowmod(num, pow, mod)
69
- num.to_bn.mod_exp(pow, mod)
70
- end
70
+ def mybcpowmod(num, pow, mod)
71
+ num.to_bn.mod_exp(pow, mod)
72
+ end
71
73
 
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)
79
- cp = c % p
80
- cq = c % q
81
- a = mybcpowmod(cp, dP, p)
82
- b = mybcpowmod(cq, dQ, q)
83
- if a > b
84
- result = a - b
85
- else
86
- result = b - a
87
- result = p - result
74
+ def rsa_encrypt(private_key, input)
75
+ p = bin2int(private_key[:prime1])
76
+ q = bin2int(private_key[:prime2])
77
+ u = bin2int(private_key[:coefficient])
78
+ dP = bin2int(private_key[:prime_exponent1])
79
+ dQ = bin2int(private_key[:prime_exponent2])
80
+ c = bin2int(input)
81
+ cp = c % p
82
+ cq = c % q
83
+ a = mybcpowmod(cp, dP, p)
84
+ b = mybcpowmod(cq, dQ, q)
85
+ if a > b
86
+ result = a - b
87
+ else
88
+ result = b - a
89
+ result = p - result
90
+ end
91
+ result = result % p
92
+ result = result * u
93
+ result = result % p
94
+ result = result * q
95
+ result = result + b
96
+ ret = bcdechex(result)
97
+ ret = padstr(ret).upcase
98
+ ret.size == 256 ? ret : false
99
+ end
88
100
  end
89
- result = result % p
90
- result = result * u
91
- result = result % p
92
- result = result * q
93
- result = result + b
94
- ret = bcdechex(result)
95
- ret = padstr(ret).upcase
96
- ret.size == 256 ? ret : false
97
- end
98
101
 
99
- class Netpayclient
100
102
  def initialize(path: nil, hash: {})
101
103
  require 'iniparse'
102
104
  @private_key = {}
@@ -117,7 +119,7 @@ module Netpayclient
117
119
  else
118
120
  raise 'config error'
119
121
  end
120
- bin = hex2bin(hex)
122
+ bin = self.class.hex2bin(hex)
121
123
  @private_key[:modulus] = bin[0,128]
122
124
 
123
125
  prime1 = bin[384,64]
@@ -138,20 +140,20 @@ module Netpayclient
138
140
  end
139
141
 
140
142
  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
143
+ check = self.class.bchexdec(input)
144
+ modulus = self.class.bin2int(@private_key[:modulus])
145
+ exponent = self.class.bchexdec("010001")
146
+ result = self.class.mybcpowmod(check, exponent, modulus)
147
+ rb = self.class.bcdechex(result)
148
+ self.class.padstr(rb).upcase
147
149
  end
148
150
 
149
151
  def sign(msg)
150
152
  if not @private_key.key?(:MERID)
151
153
  return false
152
154
  end
153
- hb = sha1_128(msg)
154
- return rsa_encrypt(@private_key, hb)
155
+ hb = self.class.sha1_128(msg)
156
+ return self.class.rsa_encrypt(@private_key, hb)
155
157
  end
156
158
 
157
159
  def sign_order(merid, ordno, amount, curyid, transdate, transtype)
@@ -168,7 +170,7 @@ module Netpayclient
168
170
  def verify(plain, check)
169
171
  return false if not @private_key.key?(:PGID)
170
172
  return false if check.size != 256
171
- hb = sha1_128(plain)
173
+ hb = self.class.sha1_128(plain)
172
174
  hbhex = hb.unpack('H*')[0].upcase
173
175
  rbhex = rsa_decrypt(check)
174
176
  return hbhex == rbhex ? true : false
data/netpayclient.gemspec CHANGED
@@ -8,6 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Netpayclient::VERSION
9
9
  spec.authors = ["Weirong Xu"]
10
10
  spec.email = ["weirongxu.raidou@gmail.com"]
11
+ spec.licenses = ['MIT']
11
12
 
12
13
  spec.summary = %q{银联商户会员的ruby SDK}
13
14
  spec.description = %q{银联商户会员的ruby SDK}
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weirong Xu
@@ -98,7 +98,8 @@ files:
98
98
  - lib/netpayclient/version.rb
99
99
  - netpayclient.gemspec
100
100
  homepage: https://github.com/weirongxu/netpayclient
101
- licenses: []
101
+ licenses:
102
+ - MIT
102
103
  metadata: {}
103
104
  post_install_message:
104
105
  rdoc_options: []