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 +4 -4
- data/lib/netpayclient/version.rb +1 -1
- data/lib/netpayclient.rb +69 -67
- data/netpayclient.gemspec +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62212890bf3ca078a71659b706570f0a9dec45e3
|
4
|
+
data.tar.gz: 3ce244d5ccdd7977bc347de5986ecf1e149e7659
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5d44ab20e3fb4fae49ac7c8ea29c310b2792347f0543d87ca169cb23418e685f09eae604fe4b98a78aa084c237c84d50f5b229a0058a6e9ea9b42c7fbf7ce9c
|
7
|
+
data.tar.gz: d14ee1ca5d0fd7d490a762ec5ed30c7c342525507032619d145a81364667f6506dc810a496ccc6694b6c796710d13e0660d719fb8fea601468363718c7496d37
|
data/lib/netpayclient/version.rb
CHANGED
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
|
-
|
35
|
-
|
36
|
-
|
34
|
+
class Netpayclient
|
35
|
+
class << self
|
36
|
+
def hex2bin(hexdata)
|
37
|
+
[hexdata].pack "H*"
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
50
|
+
def bin2int(bindata)
|
51
|
+
bchexdec(bindata.unpack('H*')[0])
|
52
|
+
end
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
def bchexdec(hexdata)
|
55
|
+
hexdata.to_i(16)
|
56
|
+
end
|
55
57
|
|
56
|
-
|
57
|
-
|
58
|
-
|
58
|
+
def bcdechex(decdata)
|
59
|
+
decdata.to_s(16)
|
60
|
+
end
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
70
|
+
def mybcpowmod(num, pow, mod)
|
71
|
+
num.to_bn.mod_exp(pow, mod)
|
72
|
+
end
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
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.
|
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: []
|