-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi4things.js
468 lines (411 loc) · 14.7 KB
/
i4things.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**********************************************************\
| |
| xxtea.js |
| |
| XXTEA encryption algorithm library for JavaScript. |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <[email protected]> |
| LastModified: Oct 4, 2016 |
| (partially edited and cropped) |
| |
| License (MIT) |
\**********************************************************/
var i4things_xxtea_delta = 0x9E3779B9;
function i4things_xxtea_mx(sum, y, z, p, e, k) {
return ((z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4)) ^ ((sum ^ y) + (k[p & 3 ^ e] ^ z));
}
function i4things_xxtea_int32(i) {
return i & 0xFFFFFFFF;
}
function i4things_xxtea_toUint32Array(bs) {
var length = bs.length;
var n = length >> 2;
v = new Array(n);
for (var i = 0; i < n; ++i) {
var j = i << 2;
v[i] = ((bs[j + 3] & 0xFF) << 24) | ((bs[j + 2] & 0xFF) << 16) | ((bs[j + 1] & 0xFF) << 8) | (bs[j] & 0xFF);
}
return v;
}
function i4things_xxtea_toUint32ArraySize(bs) {
bs.unshift(bs.length);
for (var i = 0;
((bs.length < 8) || ((bs.length & 3) != 0)); i++) {
bs.push(Math.floor((Math.random() * 255)));
}
return i4things_xxtea_toUint32Array(bs);
}
function i4things_xxtea_toUint8ArraySize(v) {
var bs = new Array(v.length << 2);
for (var i = 0; i < v.length; i++) {
j = (i << 2);
bs[j + 3] = v[i] >> 24 & 0xFF;
bs[j + 2] = v[i] >> 16 & 0xFF;
bs[j + 1] = v[i] >> 8 & 0xFF;
bs[j] = v[i] & 0xFF;
}
return bs.slice(1, bs[0] + 1);
}
function i4things_xxtea_toUint8Array(v) {
var bs = new Array(v.length << 2);
for (var i = 0; i < v.length; i++) {
j = (i << 2);
bs[j + 3] = v[i] >> 24 & 0xFF;
bs[j + 2] = v[i] >> 16 & 0xFF;
bs[j + 1] = v[i] >> 8 & 0xFF;
bs[j] = v[i] & 0xFF;
}
return bs;
}
function i4things_xxtea_encryptUint32Array(v, k) {
var length = v.length;
var n = length - 1;
var y, z, sum, e, p, q;
z = v[n];
sum = 0;
for (q = Math.floor(6 + 52 / length) | 0; q > 0; --q) {
sum = i4things_xxtea_int32(sum + i4things_xxtea_delta);
e = sum >>> 2 & 3;
for (p = 0; p < n; ++p) {
y = v[p + 1];
z = v[p] = i4things_xxtea_int32(v[p] + i4things_xxtea_mx(sum, y, z, p, e, k));
}
y = v[0];
z = v[n] = i4things_xxtea_int32(v[n] + i4things_xxtea_mx(sum, y, z, n, e, k));
}
return v;
}
function i4things_xxtea_decryptUint32Array(v, k) {
var length = v.length;
var n = length - 1;
var y, z, sum, e, p, q;
y = v[0];
q = Math.floor(6 + 52 / length);
for (sum = i4things_xxtea_int32(q * i4things_xxtea_delta); sum !== 0; sum = i4things_xxtea_int32(sum - i4things_xxtea_delta)) {
e = sum >>> 2 & 3;
for (p = n; p > 0; --p) {
z = v[p - 1];
y = v[p] = i4things_xxtea_int32(v[p] - i4things_xxtea_mx(sum, y, z, p, e, k));
}
z = v[n];
y = v[0] = i4things_xxtea_int32(v[0] - i4things_xxtea_mx(sum, y, z, 0, e, k));
}
return v;
}
function i4things_xxtea_decrypt(data, key) {
if (data === undefined || data === null || data.length === 0) {
return data;
}
return i4things_xxtea_toUint8ArraySize(i4things_xxtea_decryptUint32Array(i4things_xxtea_toUint32Array(data), i4things_xxtea_toUint32Array(key)));
}
function i4things_xxtea_encrypt(data, key) {
if (data === undefined || data === null || data.length === 0) {
return data;
}
return i4things_xxtea_toUint8Array(i4things_xxtea_encryptUint32Array(i4things_xxtea_toUint32ArraySize(data), i4things_xxtea_toUint32Array(key)));
}
/**********************************************************\
Data Operations
\**********************************************************/
function i4things_parse_hex(str) {
var result = [];
while (str.length >= 2) {
result.push(parseInt(str.substring(0, 2), 16));
str = str.substring(2, str.length);
}
return result;
}
function i5things_to_hex(arr) {
return Array.from(arr, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
function i4things_long_to_byte_array( /*long*/ l) {
var ba = [0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < ba.length; i++) {
var b = l & 0xFF;
ba[i] = b;
l = (l - b) / 256;
}
return ba;
};
function i4things_byte_array_to_long( /*byte[]*/ ba) {
var v = 0;
for (var i = ba.length - 1; i >= 0; i--) {
v = (v * 256) + ba[i];
}
return v;
};
function i4things_long_random(length) {
var n = '';
while (n.length < length) {
n += ('' + Math.random()).split('.')[1];
}
return n.substr(0, length);
}
function i4things_crc4(c) {
var crc = 0;
for (var i = 0; i < c.length; i++) {
var b = c[i] & 0xFF;
crc = (crc << 1) ^ b;
crc = crc & 0xFFFFFFFF;
};
return crc;
}
function i4things_crc(c) {
var crc = 8606;
for (var i = 0; i < c.length; i++) {
var b = c[i] & 0xFF;
crc = (crc << 1) ^ b;
crc = crc & 0xFFFFFFFF;
};
return crc & 0xFF;
}
function i4_things_challenge(network_key) {
var t = new Date().getTime();
var c = i4things_long_to_byte_array(t);
// gen CRC
var crc = i4things_crc4(c);
// and in front of challenge
c.unshift(crc & 0xFF, crc >> 8 & 0xFF, crc >> 16 & 0xFF, crc >> 24 & 0xFF);
// encrypt
return i4things_xxtea_encrypt(c, i4things_parse_hex(network_key));
}
// id integer. network_key in HEX format 32 chars
function i4things_get_data_request(id, network_key) {
// gen challenge
var c = i4_things_challenge(network_key);
return id.toString() + '-' + i5things_to_hex(c).toUpperCase() + '-' + i4things_long_random(20);
}
// id integer, data byte array, network_key in HEX format 32 chars, private_key 16 bytes array
function i4things_set_data_request(id, data, network_key, private_key) {
// gen challenge
var c = i4_things_challenge(network_key);
var crc = i4things_crc(data);
data.unshift(crc);
return id.toString() + '-' + i5things_to_hex(c).toUpperCase() + '-' + i5things_to_hex(i4things_xxtea_encrypt(data, private_key)).toUpperCase() + "-" + i4things_long_random(20);
}
// id integer, hist day index integer, network_key in HEX format 32 chars
function i4things_get_data_hist_request(id, day_idx, network_key) {
// gen challenge
var c = i4_things_challenge(network_key);
return id.toString() + '-' + day_idx.toString() + '-' + i5things_to_hex(c).toUpperCase() + '-' + i4things_long_random(20);
}
/**********************************************************\
Dynamic load script functions
\**********************************************************/
function i4things_load_script(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function() {
if (script.readyState == "loaded" ||
script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function() {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
// return
//{
// pos: new_pos,
// value:new_value
//};
function i4things_get_ots(buf, pos) {
var start = pos;
switch (buf[start] & 0x3) {
case 0:
{
var new_pos = pos + 1;
var new_value = Math.trunc(buf[start] / 4);
return {
pos: new_pos,
value: new_value
};
}
case 1:
{
var new_pos = pos + 2;
var new_value = Math.trunc(((buf[start + 1] * 256) + buf[start]) / 4);
return {
pos: new_pos,
value: new_value
};
}
case 2:
{
var new_pos = pos + 4;
var new_value = Math.trunc(((buf[start + 3] * 16777216) + (buf[start + 2] * 65536) + (buf[start + 1] * 256) + buf[start]) / 4);
return {
pos: new_pos,
value: new_value
};
}
case 3:
{
var new_pos = pos + 8;
var new_value =Math.trunc( ((((buf[start + 7] * 16777216) + (buf[start + 6] * 65536) + (buf[start + 5] * 256) + buf[start + 4]) * 4294967296) + // 2 ^ 32
((buf[start + 3] * 16777216) + (buf[start + 2] * 65536) + (buf[start + 1] * 256) + buf[start])) / 4);
return {
pos: new_pos,
value: new_value
};
}
}
}
function i4things_put_ots(v, buf) {
if (v < 0x40) {
v = (v << 2);
buf.push(v & 0xFF);
} else if (v < 0x4000) {
v = (v << 2);
v = v | 1;
buf.push(v & 0xFF, (v >> 8) & 0xFF);
} else if (v < 0x40000000) {
v = (v << 2);
v = v | 2;
buf.push(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF);
} else if (v < 0x4000000000000000) {
v = (v * 4);
v = v + 3;
buf.push(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF);
v = v / 4294967296; // 2 ^ 32
buf.push(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF);
}
}
/**********************************************************\
Value helper functions
\**********************************************************/
//add discrete data
//use this if you want for example to store decimal(floating-point) temperature between -20 and +60 in one byte
// pos is the position from which the data will be written
// e.g. : add_discrete(buf, 0, -20.0, 60.0, 23.65, 1);
//you need to ensure that you at least have container_size bytes available in the buffer
//1 <= conainer_size <= 4
//on return pos will have the value of first free byte(next postion after the data)
function i4things_add_discrete(buf, min, max, value, container_size) {
var dev;
switch (container_size) {
case 1:
{
dev = (max - min) / 255.0;
var dis = Math.round((value - min) / dev);
buf.push(dis & 0xFF);
};
break;
case 2:
{
dev = (max - min) / 65535.0;
var dis = Math.round((value - min) / dev);
buf.push(dis & 0xFF, (dis >> 8) & 0xFF);
};
break;
case 3:
{
dev = (max - min) / 16777215.0;
var dis = Math.round((value - min) / dev);
buf.push(dis & 0xFF, (dis >> 8) & 0xFF, (dis >> 16) & 0xFF);
};
break;
case 4:
{
dev = (max - min) / 4294967295.0;
var dis = Math.round((value - min) / dev);
buf.push(dis & 0xFF, (dis >> 8) & 0xFF, (dis >> 16) & 0xFF, (dis >> 24) & 0xFF);
};
break;
default:
{
dev_ = (max - min) / 255.0;
var dis = Math.round((value - min) / dev);
buf.push(dis & 0xFF);
}
};
}
// return
//{
// pos: new_pos,
// value:new_value
//};
function i4things_get_discrete(buf, pos, min, max, container_size) {
var dev;
switch (container_size) {
case 1:
{
dev = (max - min) / 255.0;
var new_pos = pos + 1;
var new_value = (min + (buf[pos] * dev));
return {
pos: new_pos,
value: new_value
};
};
break;
case 2:
{
dev = (max - min) / 65535.0;
var new_pos = pos + 2;
var new_value = (min + ((buf[pos] + (buf[pos + 1] * 256)) * dev));
return {
pos: new_pos,
value: new_value
};
};
break;
case 3:
{
dev = (max - min) / 16777215.0;
var new_pos = pos + 3;
var new_value = (min + ((buf[pos] + (buf[pos + 1] * 256) + (buf[pos + 2] * 65536)) * dev));
return {
pos: new_pos,
value: new_value
};
};
break;
case 4:
{
dev = (max - min) / 4294967295.0;
var new_pos = pos + 3;
var new_value = (min + ((buf[pos] + (buf[pos + 1] * 256) + (buf[pos + 2] * 65536) + (buf[pos + 3] * 16777216)) * dev));
return {
pos: new_pos,
value: new_value
};
};
break;
default: {
dev = (max - min) / 255.0;
var new_pos = pos + 1;
var new_value = (min + (buf[pos] * dev));
return {
pos: new_pos,
value: new_value
};
}
};
}
//add unsigned integer data up to 62bit ( 0 - 4611686018427387904) - the container will be adjusted automatically depending on the value
//you need to ensure that you have at least 8 bytes available in the buffer - as this is the maximum bytes that the number can occupy in worst case scenario
function i4things_add_uint(buf, value) {
i4things_put_ots(value, buf);
}
// return
//{
// pos: new_pos,
// value:new_value
//};
function i4things_get_uint(buf, pos) {
return i4things_get_ots(buf, pos);
}