-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmssdkbyte.ts
75 lines (63 loc) · 1.89 KB
/
mssdkbyte.ts
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
class Webmssdk {
private MAGIC1 = 1213091658;
private MAGIC2 = 1077891651;
genCode(code: string, r: number): number[] {
var t = parseInt(code.slice(r, r + 2), 16);
if (t >>> 7 == 0) {
return [1, t]
} else if (t >>> 6 == 2) {
t = (63 & t) << 8;
return [2, t += parseInt(code.slice(r + 2, r + 4), 16)]
} else {
t = (63 & t) << 16;
return [3, t += parseInt(code.slice(r + 2, r + 6), 16)]
}
}
genKey(str: string) {
let key = 0;
for (let n = 0; n < 4; ++n) {
key += (3 & parseInt(str.slice(24 + 2 * n, 26 + 2 * n), 16)) << 2 * n;
}
//console.log("key", key);
return key;
}
decodeStr(str: string, u: number, b: number, key: number) {
const data: string[] = [];
for (let n = 0; n < b; ++n) {
var p = this.genCode(str, u);
//console.log(p);
u += 2 * p[0];
let h = "";
for (let chrIndex = 0; chrIndex < p[1]; ++chrIndex) {
var x = this.genCode(str, u);
h += String.fromCharCode(key ^ x[1]), u += 2 * x[0];
}
data.push(h);
}
//console.log(i);
return data;
}
chkMagic(m_1: number, m_2: number) {
if (this.MAGIC1 !== m_1 || this.MAGIC2 !== m_2) {
throw new Error("Magic error...");
}
}
validateStr(str: string) {
if (0 !== parseInt(str.slice(16, 18), 16)) {
throw new Error("Invalid string");
}
}
decrypt(str: string) {
const m_1 = parseInt(str.slice(0, 8), 16);
const m_2 = parseInt(str.slice(8, 16), 16);
this.chkMagic(m_1, m_2);
this.validateStr(str);
const key = this.genKey(str);
var l = 2 * parseInt(str.slice(48, 56), 16);
var u = l + 56, b = parseInt(str.slice(u, u + 4), 16);
const strARR = this.decodeStr(str, u + 4, b, key);
return strARR;
}
}
const webmssdk = new Webmssdk();
export default webmssdk;