-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
62 lines (41 loc) · 1.18 KB
/
util.py
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
from struct import (
pack,
unpack,
)
from ripemd128 import ripemd128
def uint_from_byte_be(b):
return int.from_bytes(b, 'big')
def uint_from_byte_le(b):
return int.from_bytes(b, 'little')
def part(sequence, offset, length):
return sequence[slice(offset, offset + length)]
def part_0(sequence, offset):
i = offset
while sequence[i] != 0:
i += 1
return sequence[slice(offset, i + 1)]
def part_rn0(sequence, offset):
i = offset
while part(sequence, i, 3) != b'\r\n\x00':
i += 3
return sequence[slice(offset, i + 3)]
def uint_be(*args, **kwargs):
p = part(*args, **kwargs)
return uint_from_byte_be(p)
def uint_le(*args, **kwargs):
p = part(*args, **kwargs)
return uint_from_byte_le(p)
def _fast_decrypt(data, key):
b = bytearray(data)
k = bytearray(key)
p = 0x36
for i in range(len(b)):
t = (b[i] >> 4 | b[i] << 4) & 0xff
t = t ^ p ^ (i & 0xff) ^ k[i % len(k)]
p = b[i]
b[i] = t
return bytes(b)
# TODO: rename
def _mdx_decrypt(comp_block):
key = ripemd128(comp_block[4:8] + pack(b'<L', 0x3695))
return comp_block[0:8] + _fast_decrypt(comp_block[8:], key)