Skip to content

Commit f0357be

Browse files
committed
Delete nonsence code.
1 parent 86d5134 commit f0357be

2 files changed

Lines changed: 20 additions & 149 deletions

File tree

src/Berrysoft.Tsinghua.Net/AuthHelper.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private async Task<string> GetChallengeAsync()
9393
return match.Groups[1].Value;
9494
}
9595

96-
private Dictionary<string, string> loginDataDictionary;
96+
private Dictionary<string, string> logDataDictionary;
9797
private const string LoginInfoJson = "{{\"ip\": \"\", \"acid\": \"1\", \"enc_ver\": \"srun_bx1\", \"username\": \"{0}\", \"password\": \"{1}\"}}";
9898
private const string ChkSumData = "{0}{1}{0}{2}{0}1{0}{0}200{0}1{0}{3}";
9999
/// <summary>
@@ -105,22 +105,22 @@ private async Task<Dictionary<string, string>> GetLoginDataAsync()
105105
//const string passwordMD5 = "5e543256c480ac577d30f76f9120eb74";
106106
string token = await GetChallengeAsync();
107107
string passwordMD5 = CryptographyHelper.GetHMACMD5(token);
108-
if (loginDataDictionary == null)
108+
if (logDataDictionary == null)
109109
{
110-
loginDataDictionary = new Dictionary<string, string>
110+
logDataDictionary = new Dictionary<string, string>
111111
{
112-
["action"] = "login",
113112
["ac_id"] = "1",
114113
["double_stack"] = "1",
115114
["n"] = "200",
116-
["type"] = "1",
117-
["password"] = "{MD5}" + passwordMD5
115+
["type"] = "1"
118116
};
119117
}
120-
loginDataDictionary["info"] = "{SRBX1}" + CryptographyHelper.Base64Encode(CryptographyHelper.XEncode(string.Format(LoginInfoJson, Username, Password), token));
121-
loginDataDictionary["username"] = Username;
122-
loginDataDictionary["chksum"] = CryptographyHelper.GetSHA1(string.Format(ChkSumData, token, Username, passwordMD5, loginDataDictionary["info"]));
123-
return loginDataDictionary;
118+
logDataDictionary["action"] = "login";
119+
logDataDictionary["password"] = "{MD5}" + passwordMD5;
120+
logDataDictionary["info"] = "{SRBX1}" + CryptographyHelper.Base64Encode(CryptographyHelper.XEncode(string.Format(LoginInfoJson, Username, Password), token));
121+
logDataDictionary["username"] = Username;
122+
logDataDictionary["chksum"] = CryptographyHelper.GetSHA1(string.Format(ChkSumData, token, Username, passwordMD5, logDataDictionary["info"]));
123+
return logDataDictionary;
124124
}
125125
}
126126
/// <summary>

src/Berrysoft.Tsinghua.Net/CryptographyHelper.cs

Lines changed: 10 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -205,150 +205,21 @@ public unsafe static string Base64Encode(string t)
205205
}
206206
return new string(u, 0, len);
207207
}
208-
209-
private static unsafe uint[] RStr2Binl(string input)
210-
{
211-
int len = input.Length;
212-
uint[] output = new uint[(len + 3) / 4];
213-
fixed (uint* pv = output)
214-
{
215-
byte* pb = (byte*)pv;
216-
for (int i = 0; i < len; i++)
217-
{
218-
pb[i] = (byte)input[i];
219-
}
220-
}
221-
return output;
222-
}
223-
224-
private static unsafe string Binl2RStr(uint[] input)
225-
{
226-
int l = input.Length * 4;
227-
fixed (uint* pa = input)
228-
{
229-
byte* pb = (byte*)pa;
230-
char[] aa = new char[l];
231-
for (int i = 0; i < l; i++)
232-
{
233-
aa[i] = (char)pb[i];
234-
}
235-
return new string(aa);
236-
}
237-
}
238-
239-
private static int BitRol(uint num, int cnt) { return (int)((num << cnt) | (num >> (32 - cnt))); }
240-
private static int MD5Cmn(int q, int a, int b, int x, int s, int t) { return BitRol((uint)(a + q + x + t), s) + b; }
241-
private static int MD5ff(int a, int b, int c, int d, uint x, int s, int t) { return MD5Cmn((b & c) | ((~b) & d), a, b, (int)x, s, t); }
242-
private static int MD5gg(int a, int b, int c, int d, uint x, int s, int t) { return MD5Cmn((b & d) | (c & (~d)), a, b, (int)x, s, t); }
243-
private static int MD5hh(int a, int b, int c, int d, uint x, int s, int t) { return MD5Cmn(b ^ c ^ d, a, b, (int)x, s, t); }
244-
private static int MD5ii(int a, int b, int c, int d, uint x, int s, int t) { return MD5Cmn(c ^ (b | (~d)), a, b, (int)x, s, t); }
245-
246-
private static uint[] Binl(List<uint> x, int len)
208+
/// <summary>
209+
/// Get HMAC-MD5 computed string with a key and an empty string by <see cref="HMACMD5"/>.
210+
/// </summary>
211+
/// <param name="key">Original key.</param>
212+
/// <returns>An encoded string.</returns>
213+
public static string GetHMACMD5(string key)
247214
{
248-
int a = 1732584193, b = -271733879, c = -1732584194, d = 271733878;
249-
int index = len >> 5;
250-
while (x.Count <= index)
251-
x.Add(0);
252-
x[index] |= (uint)(0x80 << (len % 32));
253-
index = (((len + 64) >> 9) << 4) + 14;
254-
while (x.Count < index)
255-
x.Add(0);
256-
x.Add((uint)len);
257-
while (x.Count % 16 != 0)
258-
x.Add(0);
259-
for (int i = 0; i < x.Count; i += 16)
215+
if (string.IsNullOrEmpty(key))
260216
{
261-
int olda = a, oldb = b, oldc = c, oldd = d;
262-
263-
a = MD5ff(a, b, c, d, x[i + 0], 7, -680876936);
264-
d = MD5ff(d, a, b, c, x[i + 1], 12, -389564586);
265-
c = MD5ff(c, d, a, b, x[i + 2], 17, 606105819);
266-
b = MD5ff(b, c, d, a, x[i + 3], 22, -1044525330);
267-
a = MD5ff(a, b, c, d, x[i + 4], 7, -176418897);
268-
d = MD5ff(d, a, b, c, x[i + 5], 12, 1200080426);
269-
c = MD5ff(c, d, a, b, x[i + 6], 17, -1473231341);
270-
b = MD5ff(b, c, d, a, x[i + 7], 22, -45705983);
271-
a = MD5ff(a, b, c, d, x[i + 8], 7, 1770035416);
272-
d = MD5ff(d, a, b, c, x[i + 9], 12, -1958414417);
273-
c = MD5ff(c, d, a, b, x[i + 10], 17, -42063);
274-
b = MD5ff(b, c, d, a, x[i + 11], 22, -1990404162);
275-
a = MD5ff(a, b, c, d, x[i + 12], 7, 1804603682);
276-
d = MD5ff(d, a, b, c, x[i + 13], 12, -40341101);
277-
c = MD5ff(c, d, a, b, x[i + 14], 17, -1502002290);
278-
b = MD5ff(b, c, d, a, x[i + 15], 22, 1236535329);
279-
280-
a = MD5gg(a, b, c, d, x[i + 1], 5, -165796510);
281-
d = MD5gg(d, a, b, c, x[i + 6], 9, -1069501632);
282-
c = MD5gg(c, d, a, b, x[i + 11], 14, 643717713);
283-
b = MD5gg(b, c, d, a, x[i + 0], 20, -373897302);
284-
a = MD5gg(a, b, c, d, x[i + 5], 5, -701558691);
285-
d = MD5gg(d, a, b, c, x[i + 10], 9, 38016083);
286-
c = MD5gg(c, d, a, b, x[i + 15], 14, -660478335);
287-
b = MD5gg(b, c, d, a, x[i + 4], 20, -405537848);
288-
a = MD5gg(a, b, c, d, x[i + 9], 5, 568446438);
289-
d = MD5gg(d, a, b, c, x[i + 14], 9, -1019803690);
290-
c = MD5gg(c, d, a, b, x[i + 3], 14, -187363961);
291-
b = MD5gg(b, c, d, a, x[i + 8], 20, 1163531501);
292-
a = MD5gg(a, b, c, d, x[i + 13], 5, -1444681467);
293-
d = MD5gg(d, a, b, c, x[i + 2], 9, -51403784);
294-
c = MD5gg(c, d, a, b, x[i + 7], 14, 1735328473);
295-
b = MD5gg(b, c, d, a, x[i + 12], 20, -1926607734);
296-
297-
a = MD5hh(a, b, c, d, x[i + 5], 4, -378558);
298-
d = MD5hh(d, a, b, c, x[i + 8], 11, -2022574463);
299-
c = MD5hh(c, d, a, b, x[i + 11], 16, 1839030562);
300-
b = MD5hh(b, c, d, a, x[i + 14], 23, -35309556);
301-
a = MD5hh(a, b, c, d, x[i + 1], 4, -1530992060);
302-
d = MD5hh(d, a, b, c, x[i + 4], 11, 1272893353);
303-
c = MD5hh(c, d, a, b, x[i + 7], 16, -155497632);
304-
b = MD5hh(b, c, d, a, x[i + 10], 23, -1094730640);
305-
a = MD5hh(a, b, c, d, x[i + 13], 4, 681279174);
306-
d = MD5hh(d, a, b, c, x[i + 0], 11, -358537222);
307-
c = MD5hh(c, d, a, b, x[i + 3], 16, -722521979);
308-
b = MD5hh(b, c, d, a, x[i + 6], 23, 76029189);
309-
a = MD5hh(a, b, c, d, x[i + 9], 4, -640364487);
310-
d = MD5hh(d, a, b, c, x[i + 12], 11, -421815835);
311-
c = MD5hh(c, d, a, b, x[i + 15], 16, 530742520);
312-
b = MD5hh(b, c, d, a, x[i + 2], 23, -995338651);
313-
314-
a = MD5ii(a, b, c, d, x[i + 0], 6, -198630844);
315-
d = MD5ii(d, a, b, c, x[i + 7], 10, 1126891415);
316-
c = MD5ii(c, d, a, b, x[i + 14], 15, -1416354905);
317-
b = MD5ii(b, c, d, a, x[i + 5], 21, -57434055);
318-
a = MD5ii(a, b, c, d, x[i + 12], 6, 1700485571);
319-
d = MD5ii(d, a, b, c, x[i + 3], 10, -1894986606);
320-
c = MD5ii(c, d, a, b, x[i + 10], 15, -1051523);
321-
b = MD5ii(b, c, d, a, x[i + 1], 21, -2054922799);
322-
a = MD5ii(a, b, c, d, x[i + 8], 6, 1873313359);
323-
d = MD5ii(d, a, b, c, x[i + 15], 10, -30611744);
324-
c = MD5ii(c, d, a, b, x[i + 6], 15, -1560198380);
325-
b = MD5ii(b, c, d, a, x[i + 13], 21, 1309151649);
326-
a = MD5ii(a, b, c, d, x[i + 4], 6, -145523070);
327-
d = MD5ii(d, a, b, c, x[i + 11], 10, -1120210379);
328-
c = MD5ii(c, d, a, b, x[i + 2], 15, 718787259);
329-
b = MD5ii(b, c, d, a, x[i + 9], 21, -343485551);
330-
331-
a += olda;
332-
b += oldb;
333-
c += oldc;
334-
d += oldd;
217+
key = string.Empty;
335218
}
336-
return new uint[] { (uint)a, (uint)b, (uint)c, (uint)d };
337-
}
338-
339-
public static string GetHMACMD5(string key)
340-
{
341-
var bkey = RStr2Binl(key);
342-
uint[] ipad = new uint[16];
343-
uint[] opad = new uint[16];
344-
for (int i = 0; i < 16; i++)
219+
using (HMACMD5 hash = new HMACMD5(Encoding.UTF8.GetBytes(key)))
345220
{
346-
ipad[i] = bkey[i] ^ 0x36363636;
347-
opad[i] = bkey[i] ^ 0x5C5C5C5C;
221+
return GetHexString(hash.ComputeHash(new byte[0]));
348222
}
349-
var hash = Binl(ipad.ToList(), 512);
350-
string result = Binl2RStr(Binl(opad.Concat(hash).ToList(), 512 + 128));
351-
return GetHexString(Encoding.UTF8.GetBytes(result));
352223
}
353224
}
354225
}

0 commit comments

Comments
 (0)