Last updated: 2023-10-13 11:19:55Download PDF
UserSigis a security signature designed by Tencent Cloud to prevent attackers from accessing your Tencent Cloud account. Currently, Tencent Cloud services including TRTC, Chat, and MLVB all use this security mechanism. To use these services, you must pass in three parameters –
UserSig– to the initialization or login API of the corresponding SDK.
SDKAppIDidentifies an application, and
UserIDidentifies a user.
UserSigis a security signature calculated based on
UserIDusing the HMAC SHA256 encryption algorithm. Attackers cannot use your Tencent Cloud traffic as long as they don’t have
UserSig. See the figure below for how
UserSigis calculated. Basically, it involves hashing crucial information such as
// UserSig formula, in which `secretkey` is the key used to calculate UserSigusersig = hmacsha256(secretkey, (userid + sdkappid + currtime + expire +base64(userid + sdkappid + currtime + expire)))
currtimeis the current system time and
expirethe expiration time of the signature.
How do I calculate
UserSig during debugging or demo run?
If you want to quickly run the demo to try out TRTC features, you can generate
UserSigeither using our client-side sample code or in the console:
These two methods are only suitable for debugging. It’s not recommended for official launch because
SECRETKEYin the client code (especially on the web) may be easily decompiled and reversed. If your key is leaked, attackers can steal your Tencent Cloud traffic.
The correct method is to deploy the
UserSigcalculation code on your project server so that your application can request from your server a
UserSigthat is calculated whenever one is needed.
UserSig using client-side sample code
1. Get the
SDKAppIDand key: 1. Log in to the TRTC console and click Application Management. 2. Find your application and click Configuration. 3. In Basic information, SDKSecretKey is the key used to calculate
UserSig. 4. Copy the key.
UserSig: We offer source code for calculating
UserSigon different platforms.
We provide an open-source module called
GenerateTestUserSigin the TRTC SDK sample code. Set the three member variables of
SECRETKEY, and you will be able to call
genTestUserSig()to obtain the
UserSigand get started quickly.
UserSig in the console
1. Log in to the TRTC console, select Application Management on the left sidebar, and click UserSig generation.
2. Select your application (
SDKAppID) from the drop-down list. A secret key will be generated automatically.
3. Enter the user ID.
4. Click Generate.
How do I calculate
UserSig in a production environment?
In a production environment, server-side
UserSigcalculation offers stronger protection against key leakage because it is more difficult to hack a server than it is to reverse engineer an application. See below for detailed directions:
1. Before your application calls the initialization API of the SDK, request
UserSigfrom your server.
2. Your server will calculate a
UserSigbased on the
UserID. The calculation source code is provided above.
3. The server returns the
UserSigto your application.
4. Your application sends the
UserSigto the SDK through a specific API.
5. The SDK submits the
SDKAppID + UserID + UserSigto the Tencent Cloud server for verification.
6. Tencent Cloud verifies the validity of the
7. If the
UserSigis valid, services will be provided to the TRTC SDK.
To simplify your implementation process, we provide
UserSigcalculation source code (new algorithm) in multiple languages.
UserSig calculation source code using the legacy algorithm
To simplify the signature calculation process and facilitate your use of Tencent Cloud services, on July 19, 2019, TRTC switched from ECDSA-SHA256 to the new signature algorithm HMAC-SHA256. This means that all applications (
SDKAppID) created on and after July 19, 2019 will use the new HMAC-SHA256 algorithm.
If your application (
SDKAppID) was created before July 19, 2019, you can continue to use the old signature algorithm, whose source code can be downloaded below.