-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathGenerateTestUserSig.dart
More file actions
119 lines (105 loc) · 4.12 KB
/
GenerateTestUserSig.dart
File metadata and controls
119 lines (105 loc) · 4.12 KB
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
/// Module: GenerateTestUserSig
///
/// Description: generates UserSig for testing. UserSig is a security signature designed by Tencent Cloud for its cloud services.
/// It is calculated based on `SDKAppID`, `UserID`, and `EXPIRETIME` using the HMAC-SHA256 encryption algorithm.
///
/// Attention: do not use the code below in your commercial app. This is because:
///
/// The code may be able to calculate UserSig correctly, but it is only for quick testing of the SDK’s basic features, not for commercial apps.
/// `SDKSECRETKEY` in client code can be easily decompiled and reversed, especially on web.
/// Once your key is disclosed, attackers will be able to steal your Tencent Cloud traffic.
///
/// The correct method is to deploy the `UserSig` calculation code and encryption key on your project server so that your app can request from your server a `UserSig` that is calculated whenever one is needed.
/// Given that it is more difficult to hack a server than a client app, server-end calculation can better protect your key.
///
/// Reference: https://cloud.tencent.com/document/product/647/17275#Server
// ignore_for_file: slash_for_doc_comments
import 'dart:convert';
import 'dart:io';
import 'package:crypto/crypto.dart';
class GenerateTestUserSig {
/**
* Tencent Cloud `SDKAppID`. Set it to the `SDKAppID` of your account.
*
* You can view your `SDKAppID` after creating an application in the [TRTC console](https://console.cloud.tencent.com/trtc).
* `SDKAppID` uniquely identifies a Tencent Cloud account.
*/
static int sdkAppId = 0;
/**
* Signature validity period, which should not be set too short
* <p>
* Unit: second
* Default value: 604800 (7 days)
*/
static int expireTime = 604800;
/**
* Follow the steps below to obtain the key required for UserSig calculation.
*
* Step 1. Log in to the [TRTC console](https://console.cloud.tencent.com/trtc), and create an application if you don’t have one.
* Step 2. Find your application, click “Application Info”, and click the “Quick Start” tab.
* Step 3. Copy and paste the key to the code, as shown below.
*
* Note: this method is for testing only. Before commercial launch, please migrate the UserSig calculation code and key to your backend server to prevent key disclosure and traffic stealing.
* Reference: https://cloud.tencent.com/document/product/647/17275#Server
*/
static String sdkSecretKey = '';
/**
* CDN发布功能 混流appId
*/
/**
* `appId` for CDN publishing and stream mixing
*/
static int appId = 1256635546;
/**
* CDN发布功能 混流bizId
*/
/**
* `bizId` for CDN publishing and stream mixing
*/
static int bizId = 93434;
static genTestSig(String userId) {
int currTime = _getCurrentTime();
String sig = '';
Map<String, dynamic> sigDoc = new Map<String, dynamic>();
sigDoc.addAll({
"TLS.ver": "2.0",
"TLS.identifier": userId,
"TLS.sdkappid": sdkAppId,
"TLS.expire": expireTime,
"TLS.time": currTime,
});
sig = _hmacsha256(
identifier: userId,
currTime: currTime,
expire: expireTime,
);
sigDoc['TLS.sig'] = sig;
String jsonStr = json.encode(sigDoc);
List<int> compress = zlib.encode(utf8.encode(jsonStr));
return _escape(content: base64.encode(compress));
}
static int _getCurrentTime() {
return (DateTime.now().millisecondsSinceEpoch / 1000).floor();
}
static String _hmacsha256({
required String identifier,
required int currTime,
required int expire,
}) {
int sdkappid = sdkAppId;
String contentToBeSigned =
"TLS.identifier:$identifier\nTLS.sdkappid:$sdkappid\nTLS.time:$currTime\nTLS.expire:$expire\n";
Hmac hmacSha256 = new Hmac(sha256, utf8.encode(sdkSecretKey));
Digest hmacSha256Digest =
hmacSha256.convert(utf8.encode(contentToBeSigned));
return base64.encode(hmacSha256Digest.bytes);
}
static String _escape({
required String content,
}) {
return content
.replaceAll('\+', '*')
.replaceAll('\/', '-')
.replaceAll('=', '_');
}
}