]> code.delx.au - pymsnt/blob - src/legacy/msn/msnp11chl.py
* Python 2.5 doesn't always return longs from struct.unpack, so we have to be clever...
[pymsnt] / src / legacy / msn / msnp11chl.py
1 # Copyright 2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 import md5
5 import struct
6
7 MSNP11_PRODUCT_ID = "PROD0090YUAUV{2B"
8 MSNP11_PRODUCT_KEY = "YMM8C_H7KCQ2S_KL"
9 MSNP11_MAGIC_NUM = 0x0E79A9C1
10
11
12 def doChallenge(chlData):
13 md5digest = md5.md5(chlData + MSNP11_PRODUCT_KEY).digest()
14
15 # Make array of md5 string ints
16 md5Ints = struct.unpack("<llll", md5digest)
17 md5Ints = [(x & 0x7fffffff) for x in md5Ints]
18
19 # Make array of chl string ints
20 chlData += MSNP11_PRODUCT_ID
21 amount = 8 - len(chlData) % 8
22 chlData += "".zfill(amount)
23 chlInts = struct.unpack("<%di" % (len(chlData)/4), chlData)
24
25 # Make the key
26 high = 0
27 low = 0
28 i = 0
29 while i < len(chlInts) - 1:
30 temp = chlInts[i]
31 temp = (MSNP11_MAGIC_NUM * temp) % 0x7FFFFFFF
32 temp += high
33 temp = md5Ints[0] * temp + md5Ints[1]
34 temp = temp % 0x7FFFFFFF
35
36 high = chlInts[i + 1]
37 high = (high + temp) % 0x7FFFFFFF
38 high = md5Ints[2] * high + md5Ints[3]
39 high = high % 0x7FFFFFFF
40
41 low = low + high + temp
42
43 i += 2
44
45 high = littleEndify((high + md5Ints[1]) % 0x7FFFFFFF)
46 low = littleEndify((low + md5Ints[3]) % 0x7FFFFFFF)
47 key = (high << 32L) + low
48 key = littleEndify(key, "Q")
49
50 longs = [x for x in struct.unpack(">QQ", md5digest)]
51 longs = [littleEndify(x, "Q") for x in longs]
52 longs = [x ^ key for x in longs]
53 longs = [littleEndify(abs(x), "Q") for x in longs]
54
55 out = ""
56 for x in longs:
57 x = hex(x)
58 x = x[2:].strip("L")
59 x = x.zfill(16)
60 out += x.lower()
61
62 return out
63
64 def littleEndify(num, c="L"):
65 return struct.unpack(">" + c, struct.pack("<" + c, num))[0]
66
67
68 if __name__ == "__main__":
69 import sys
70 try:
71 print doChallenge(sys.argv[1])
72 except IndexError:
73 print "No args passed"
74 raise
75