dsp 0.0.2 → 0.0.3

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dsp.rb +183 -1
  3. data/spec/tea_spec.rb +13 -0
  4. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82d4835b9f6e01e0b94c5eaef086b78bf2f78f0f
4
- data.tar.gz: fc36ce71cdf17f682e76b1b2ac65ef5478633cbe
3
+ metadata.gz: 717b36b08cf9d44035073fbca03ad48f1d2ef28d
4
+ data.tar.gz: cb03032ba5582df1401fb9881475c4eb7931f46e
5
5
  SHA512:
6
- metadata.gz: cf599330a35fa473d1c28af442d8392c40d437018fe3cfa36d7bdc1e57197270823822dfa335da6d37b991a17175e5ed34a5adabab96082d099e13ba6b7a5637
7
- data.tar.gz: 9fc5fe7b098a3ac98966ef1033cfcb282958176871998079210f07be0284182e8b80424ff8fc5ce4072a2c31b7d795b94a71007c88f840f7581dd8e22f389bec
6
+ metadata.gz: dba28d78f5b3b3ff3f1cca7f9985812f37cda529843d8f87cd502179f3f63de64d7c83cce43aa05d9e30ec4cbabfeb0e160709110f76bd08c11f7cdc64580d84
7
+ data.tar.gz: 0fcdb9dc45681613bfe93a3e918c335a9b676de2b8e0189f891f1789b333e38319eb138a6c48c9d7e64099d11608eb5db2691183a1c1b6565efbe43b46da4c17
data/lib/dsp.rb CHANGED
@@ -1,13 +1,195 @@
1
1
 
2
2
  require 'httparty'
3
3
  require 'json'
4
+ require 'base64'
4
5
 
5
6
  module DSP
6
7
  autoload :API , 'dsp/api'
7
8
  autoload :BID , 'dsp/bid'
8
9
 
9
- VERSION='0.0.2'
10
+ VERSION='0.0.3'
10
11
 
11
12
  AUTH = {dsp_id: 190, token: '73553e669b7270a4934706f76a99ad36'}
12
13
  ADX_URL = 'http://opentest.adx.qq.com'
14
+
15
+ class TEA
16
+ ROUND = 16
17
+ LOG_ROUNDS = 4
18
+ DELTA = 0x9e3779b9
19
+ SALT_LEN = 2
20
+ ZERO_LEN = 7
21
+ class << self
22
+ def decrypt(encryptContent, key)
23
+ con = convertByteToInt(encryptContent)
24
+ k = convertByteToInt(key)
25
+ y = getUnsignedInt(con[0])
26
+ z = getUnsignedInt(con[1])
27
+ sum = 0
28
+ a = k[0]
29
+ b = k[1]
30
+ c = k[2]
31
+ d = k[3]
32
+ sum = getUnsignedInt(0x9e3779b9 << LOG_ROUNDS)
33
+ delta = getUnsignedInt(0x9e3779b9)
34
+ for i in (0...ROUND)
35
+ z -= (y << 4) + c ^ y + sum ^ (y >> 5) + d
36
+ z &= 0xffffffff
37
+ y -= (z << 4) + a ^ z + sum ^ (z >> 5) + b
38
+ y &= 0xffffffff
39
+ sum -= delta
40
+ sum &= 0xffffffff
41
+ end
42
+
43
+ con[0] = y | 0x0
44
+ con[1] = z | 0x0
45
+ return convertIntToByte(con)
46
+ end
47
+
48
+ def getUnsignedInt(data)
49
+ return data & 0xffffffff
50
+ end
51
+
52
+ def convertByteToInt(content)
53
+ tmp = []
54
+ result = []
55
+ len = content.length
56
+ raise "Invalid length of byte array, must be 4 times." if len % 4 != 0
57
+
58
+ for i in 0...(len / 4)
59
+ tmp[0,4] = content[i*4, 4]
60
+ result[i] = ntohl(tmp)
61
+ end
62
+
63
+ return result
64
+ end
65
+
66
+ def convertIntToByte(content)
67
+ result = []
68
+ for i in (0...content.length)
69
+ tmp = htonl(content[i])
70
+ # System.arraycopy(tmp, 0, result, i * 4, 4)
71
+ result[i*4, 4] = tmp[0, 4]
72
+ end
73
+ return result
74
+ end
75
+
76
+ def htonl(x)
77
+ res = []
78
+ for i in (0...4)
79
+ res[i] = x >> 24 & 0xff
80
+ x <<= 8
81
+ end
82
+
83
+ return res
84
+ end
85
+
86
+ def ntohl(x)
87
+ res = 0
88
+ for i in (0...4)
89
+ res <<= 8
90
+ res |= 0xff & x[i]
91
+ end
92
+
93
+ return res
94
+ end
95
+
96
+ def decryptData( pInBuf, pKey)
97
+ nBufPos = 0
98
+ dest_buf = []
99
+ zero_buf = []
100
+ cur_buf = []
101
+ nInBufLen = pInBuf.length
102
+ return nil if nInBufLen % 8 != 0 || nInBufLen < 16 || pKey.length != 16
103
+ cur_buf[0,8] = pInBuf[0,8]
104
+ dest_buf = decrypt(cur_buf, pKey)
105
+ nPadLen = dest_buf[0] & 7
106
+ textLen = nInBufLen - 1 - nPadLen - 2 - 7
107
+ pOutBufLen = textLen
108
+ pOutBuf = []
109
+
110
+ for i in (0...pOutBufLen)
111
+ pOutBuf[i] = 48
112
+ end
113
+
114
+ for i in (0...8)
115
+ zero_buf[i] = 0
116
+ end
117
+
118
+ iv_pre_crypt = zero_buf.clone
119
+ iv_cur_crypt = cur_buf.clone
120
+ nBufPos += 8
121
+ cur_buf[0, 8] = pInBuf[8, 8]
122
+ dest_i = 1
123
+ dest_i += nPadLen
124
+
125
+ i = 1
126
+ while i <= 2
127
+ if(dest_i < 8)
128
+ dest_i += 1
129
+ i += 1
130
+ elsif(dest_i == 8)
131
+ iv_pre_crypt = iv_cur_crypt.clone
132
+ iv_cur_crypt = cur_buf.clone
133
+ for j in (0...8)
134
+ return nil if (nBufPos + j >= nInBufLen)
135
+ dest_buf[j] ^= pInBuf[nBufPos + j]
136
+ end
137
+ dest_buf = decrypt(dest_buf, pKey)
138
+ nBufPos += 8
139
+ cur_buf[0, 8] = pInBuf[16, 8]
140
+ dest_i = 0
141
+ end
142
+ end
143
+ nPlainLen = pOutBufLen
144
+ i = 0
145
+ while nPlainLen > 0
146
+ if(dest_i < 8)
147
+ pOutBuf[i+=1] = dest_buf[dest_i] ^ iv_pre_crypt[dest_i]
148
+ dest_i+=1
149
+ nPlainLen-=1
150
+ elsif(dest_i == 8)
151
+ iv_pre_crypt = iv_cur_crypt.clone
152
+ iv_cur_crypt = cur_buf.clone
153
+ for j in (0...8)
154
+ return nil if(nBufPos + j >= nInBufLen)
155
+ dest_buf[j] ^= pInBuf[nBufPos + j]
156
+ end
157
+ dest_buf = decrypt(dest_buf, pKey)
158
+ nBufPos += 8
159
+ dest_i = 0
160
+ end
161
+ end
162
+ i = 1
163
+ while i <= 7
164
+ if(dest_i < 8)
165
+ if((dest_buf[dest_i] ^ iv_pre_crypt[dest_i]) != 1)
166
+ dest_i+=1
167
+ i+=1
168
+ else
169
+ return nil
170
+ end
171
+ elsif(dest_i == 8)
172
+ iv_pre_crypt = iv_cur_crypt.clone
173
+ iv_cur_crypt = cur_buf.clone
174
+ for j in (0...8)
175
+ return nil if(nBufPos + j >= nInBufLen)
176
+ dest_buf[j] ^= pInBuf[j]
177
+ end
178
+ dest_buf = decrypt(dest_buf, pKey)
179
+ nBufPos += 8
180
+ dest_i = 0
181
+ end
182
+ end
183
+
184
+ return pOutBuf
185
+ end
186
+
187
+ def decryptString(encryptedStr , key)
188
+ infoStr = Base64.decode64(encryptedStr).bytes
189
+ bKey = key.bytes
190
+ res = decryptData(infoStr, bKey)
191
+ res.pack('c*')
192
+ end
193
+ end
194
+ end
13
195
  end
data/spec/tea_spec.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::DSP::TEA do
4
+
5
+ context '#decrypt' do
6
+ it 'decrypt price for rdx and return normal price' do
7
+ key = '123456789abcdefg'
8
+ encryptedStr = 'J+qNGSqDrwK/QL3CE3t48jghTr0LdsJ3'
9
+ res = ::DSP::TEA.decryptString(encryptedStr, key)
10
+ expect(res).to eq "06.00000"
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - "姚露"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-09 00:00:00.000000000 Z
11
+ date: 2015-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -53,6 +53,7 @@ files:
53
53
  - spec/api/report_spec.rb
54
54
  - spec/api/upload_ads_info_spec.rb
55
55
  - spec/spec_helper.rb
56
+ - spec/tea_spec.rb
56
57
  homepage: ''
57
58
  licenses:
58
59
  - GNU
@@ -85,3 +86,4 @@ test_files:
85
86
  - spec/api/report_spec.rb
86
87
  - spec/api/upload_ads_info_spec.rb
87
88
  - spec/spec_helper.rb
89
+ - spec/tea_spec.rb