forked from baofengcloud/csharp-sdk
/
Token.cs
155 lines (111 loc) · 6.26 KB
/
Token.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections.Generic;
using System;
using System.Security.Cryptography;
namespace Baofeng.Cloud {
public class Token {
public static String CreateUploadToken(String accessKey, String secretKey, ServiceType serviceType, FileType fileType, UploadType uploadType, string fileName, string fileKey, Int64 fileSize, Int64 timeoutSec, string callbackUrl) {
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
jsonObj.Add("uptype", (int)uploadType);
jsonObj.Add("servicetype", (int)serviceType);
jsonObj.Add("filekey", fileKey);
jsonObj.Add("filename", fileName);
jsonObj.Add("filesize", fileSize);
jsonObj.Add("filetype", (int)fileType);
jsonObj.Add("callbackurl", callbackUrl);
return CreateToken(jsonObj, secretKey, accessKey, timeoutSec);
}
public static String CreateDeleteToken(String accessKey, String secretKey, ServiceType serviceType, string fileName, string fileKey, Int64 timeoutSec, string callbackUrl) {
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
jsonObj.Add("servicetype", (int)serviceType);
jsonObj.Add("filekey", fileKey);
jsonObj.Add("filename", fileName);
jsonObj.Add("callbackurl", callbackUrl);
return CreateToken(jsonObj, secretKey, accessKey, timeoutSec);
}
public static String CreateQueryToken(String accessKey, String secretKey, ServiceType serviceType, string fileName, string fileKey) {
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
jsonObj.Add("servicetype", (int)serviceType);
jsonObj.Add("filekey", fileKey);
jsonObj.Add("filename", fileName);
return CreateToken(jsonObj, secretKey, accessKey, /*timeoutSec*/0);
}
public static String CreateUpdateToken(String accessKey, String secretKey, ServiceType serviceType, FileType fileType, string fileName, string fileKey) {
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
jsonObj.Add("servicetype", (int)serviceType);
jsonObj.Add("filekey", fileKey);
jsonObj.Add("filename", fileName);
jsonObj.Add("filetype", (int)fileType);
return CreateToken(jsonObj, secretKey, accessKey, /*timeoutSec*/0);
}
public static String LiveCreateToken(String accessKey, String secretKey, FileType fileType, string channelName, Int64 timeoutSec) {
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
jsonObj.Add("channelname", channelName);
jsonObj.Add("ifpublic", (int)fileType);
return CreateToken(jsonObj, secretKey, accessKey, timeoutSec);
}
public static String LiveDeleteToken(String accessKey, String secretKey, string channelID, Int64 timeoutSec) {
Dictionary<string, object> jsonObj = new Dictionary<string, object>();
jsonObj.Add("channelid", channelID);
return CreateToken(jsonObj, secretKey, accessKey, timeoutSec);
}
public static String CreatePlayTokenByUrl(String accessKey, String secretKey, String playUrl, Int64 timeoutSec)
{
String fid = "";
foreach (var line in playUrl.Split('&'))
{
var kv = line.Split('=');
if (kv[0] == "fid")
fid = kv[1];
}
if (fid.Length == 0)
throw new CloudException(99, "fid not found in url!");
return CreatePlayTokenByFid(accessKey, secretKey, fid, timeoutSec);
}
public static String CreatePlayTokenByFid(String accessKey, String secretKey, string fid, Int64 timeoutSec) {
Int64 unixTime = (Int64)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
string url = "id=" + fid + "&deadline=" + (unixTime + timeoutSec);
String encodedUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(url));
byte[] sign = Sign(secretKey, encodedUrl);
String encodedSign = Convert.ToBase64String(sign);
String token = accessKey + ":" + encodedSign + ":" + encodedUrl;
return token;
}
public static String CreatePlayToken(String accessKey, String secretKey, string fid, Int64 timeoutSec)
{
return CreatePlayTokenByFid(accessKey, secretKey, fid, timeoutSec);
}
private static String CreateToken(Dictionary<string, object> jsonObj, String secretKey, String accessKey, Int64 timeoutSec) {
if (timeoutSec > 0) {
Int64 unixTime = (Int64)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
jsonObj.Add("deadline", unixTime + timeoutSec);
}
String json = Dict2Json(jsonObj);
String encodedJson = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(json));
byte[] sign = Sign(secretKey, encodedJson);
String encodedSign = Convert.ToBase64String(sign);
String token = accessKey + ":" + encodedSign + ":" + encodedJson;
return token;
}
static string Dict2Json(Dictionary<string, object> dict) {
String json = "{";
foreach (KeyValuePair<string, object> pair in dict) {
if (json.Length > 1)
json += ",";
if (pair.Value.GetType() == typeof(string))
json += "\"" + pair.Key + "\":\"" + pair.Value + "\"";
else if (pair.Value.GetType() == typeof(int) || pair.Value.GetType() == typeof(Int64))
json += "\"" + pair.Key + "\":" + pair.Value.ToString();
else {
throw new Exception("unsupported type.");
}
}
json += "}";
return json;
}
static Byte[] Sign(String secretKey, String text) {
HMACSHA1 hashFunc = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secretKey));
Byte[] hashResult = hashFunc.ComputeHash(System.Text.Encoding.ASCII.GetBytes(text));
return hashResult;
}
}//class Token
}