-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cs
60 lines (52 loc) · 1.77 KB
/
Hash.cs
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
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
namespace Ustream
{
public class Hash : Dictionary<string, string>
{
public string getHash(string secret, int ttlSeconds)
{
string hashExpire = getExpireTimestamp(ttlSeconds);
this.Add("hashExpire", hashExpire);
string signString = concatSignData(secret);
string signature = md5Sign(signString);
this.Add("hash", signature);
return toJson();
}
private string concatSignData(string secret)
{
string signData = "";
foreach (KeyValuePair<string, string> data in this)
{
signData += data.Value + "|";
}
signData += secret;
return signData;
}
private string md5Sign(string signString)
{
byte[] md5 = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(signString));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < md5.Length; i++)
{
sBuilder.Append(md5[i].ToString("x2"));
}
return sBuilder.ToString();
}
private string toJson()
{
var hashDataList = this.Select(p => new Dictionary<string, string>() { { p.Key, p.Value } });
return JsonConvert.SerializeObject(hashDataList);
}
private string getExpireTimestamp(int ttl)
{
DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long hashExpireTimestamp = (long)(DateTime.Now - sTime).TotalSeconds + ttl;
return hashExpireTimestamp.ToString();
}
}
}