Skip to content

Commit f63ebcd

Browse files
authored
Merge pull request #16 from Tomxiaobai/master
java sdk update
2 parents 0c5c9ae + 52459e5 commit f63ebcd

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

README_EN.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Note
2+
This project is the java implementation of tls-sig-api-v2. Previous asymmetric keys cannot use APIs of this version. To enable them to use APIs of this version,[see here](https://github.com/tencentyun/tls-sig-api-java).
3+
## Integration
4+
### maven
5+
``` xml
6+
<dependencies>
7+
<dependency>
8+
<groupId>com.github.tencentyun</groupId>
9+
<artifactId>tls-sig-api-v2</artifactId>
10+
<version>2.0</version>
11+
</dependency>
12+
</dependencies>
13+
```
14+
15+
### gradle
16+
```
17+
dependencies {
18+
compile 'com.github.tencentyun:tls-sig-api-v2:2.0'
19+
}
20+
```
21+
22+
### source code
23+
``` shell
24+
./gradlew -b user_build.gradle build
25+
```
26+
The generated jar can be found under `build/libs`. You can download it by yourself by relying on org.json.
27+
## use
28+
``` java
29+
import com.tencentyun.TLSSigAPIv2;
30+
31+
TLSSigAPIv2 api = new TLSSigAPIv2(1400000000, "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e");
32+
System.out.print(api.genSig("xiaojun", 180*86400));
33+
```

src/main/java/com/tencentyun/TLSSigAPIv2.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public TLSSigAPIv2(long sdkappid, String key) {
3030
* @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
3131
* @return usersig -生成的签名
3232
*/
33+
34+
/**
35+
* Function: Used to issue UserSig that is required by the TRTC and IM services.
36+
* <p>
37+
* Parameter description:
38+
*
39+
* @param userid - User ID. The value can be up to 32 bytes in length and contain letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-).
40+
* @param expire - UserSig expiration time, in seconds. For example, 86400 indicates that the generated UserSig will expire one day after being generated.
41+
* @return usersig - Generated signature.
42+
*/
3343
public String genUserSig(String userid, long expire) {
3444
return genUserSig(userid, expire, null);
3545
}
@@ -60,6 +70,33 @@ public String genUserSig(String userid, long expire) {
6070
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
6171
* @return usersig - 生成带userbuf的签名
6272
*/
73+
74+
/**
75+
* Function:
76+
* Used to issue PrivateMapKey that is optional for room entry.
77+
* PrivateMapKey must be used together with UserSig but with more powerful permission control capabilities.
78+
* - UserSig can only control whether a UserID has permission to use the TRTC service. As long as the UserSig is correct, the user with the corresponding UserID can enter or leave any room.
79+
* - PrivateMapKey specifies more stringent permissions for a UserID, including whether the UserID can be used to enter a specific room and perform audio/video upstreaming in the room.
80+
* To enable stringent PrivateMapKey permission bit verification, you need to enable permission key in TRTC console > Application Management > Application Info.
81+
*
82+
* Parameter description:
83+
*
84+
* @param userid - User ID. The value can be up to 32 bytes in length and contain letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-).
85+
* @param roomid - ID of the room to which the specified UserID can enter.
86+
* @param expire - PrivateMapKey expiration time, in seconds. For example, 86400 indicates that the generated PrivateMapKey will expire one day after being generated.
87+
* @param privilegeMap - Permission bits. Eight bits in the same byte are used as the permission switches of eight specific features:
88+
* - Bit 1: 0000 0001 = 1, permission for room creation
89+
* - Bit 2: 0000 0010 = 2, permission for room entry
90+
* - Bit 3: 0000 0100 = 4, permission for audio sending
91+
* - Bit 4: 0000 1000 = 8, permission for audio receiving
92+
* - Bit 5: 0001 0000 = 16, permission for video sending
93+
* - Bit 6: 0010 0000 = 32, permission for video receiving
94+
* - Bit 7: 0100 0000 = 64, permission for substream video sending (screen sharing)
95+
* - Bit 8: 1000 0000 = 200, permission for substream video receiving (screen sharing)
96+
* - privilegeMap == 1111 1111 == 255: Indicates that the UserID has all feature permissions of the room specified by roomid.
97+
* - privilegeMap == 0010 1010 == 42: Indicates that the UserID has only the permissions to enter the room and receive audio/video data.
98+
* @return usersig - Generate signature with userbuf
99+
*/
63100
public String genPrivateMapKey(String userid, long expire, long roomid, long privilegeMap) {
64101
byte[] userbuf = genUserBuf(userid, roomid, expire, privilegeMap, 0, ""); //生成userbuf
65102
return genUserSig(userid, expire, userbuf);
@@ -91,6 +128,34 @@ public String genPrivateMapKey(String userid, long expire, long roomid, long pri
91128
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
92129
* @return usersig - 生成带userbuf的签名
93130
*/
131+
132+
/**
133+
* Function:
134+
* Used to issue PrivateMapKey that is optional for room entry.
135+
* PrivateMapKey must be used together with UserSig but with more powerful permission control capabilities.
136+
* - UserSig can only control whether a UserID has permission to use the TRTC service. As long as the UserSig is correct, the user with the corresponding UserID can enter or leave any room.
137+
* - PrivateMapKey specifies more stringent permissions for a UserID, including whether the UserID can be used to enter a specific room and perform audio/video upstreaming in the room.
138+
* To enable stringent PrivateMapKey permission bit verification, you need to enable permission key in TRTC console > Application Management > Application Info.
139+
*
140+
* Parameter description:
141+
*
142+
*
143+
* @param userid - User ID. The value can be up to 32 bytes in length and contain letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-).
144+
* @param roomid - ID of the room to which the specified UserID can enter.
145+
* @param expire - PrivateMapKey expiration time, in seconds. For example, 86400 indicates that the generated PrivateMapKey will expire one day after being generated.
146+
* @param privilegeMap - Permission bits. Eight bits in the same byte are used as the permission switches of eight specific features:
147+
* - Bit 1: 0000 0001 = 1, permission for room creation
148+
* - Bit 2: 0000 0010 = 2, permission for room entry
149+
* - Bit 3: 0000 0100 = 4, permission for audio sending
150+
* - Bit 4: 0000 1000 = 8, permission for audio receiving
151+
* - Bit 5: 0001 0000 = 16, permission for video sending
152+
* - Bit 6: 0010 0000 = 32, permission for video receiving
153+
* - Bit 7: 0100 0000 = 64, permission for substream video sending (screen sharing)
154+
* - Bit 8: 1000 0000 = 200, permission for substream video receiving (screen sharing)
155+
* - privilegeMap == 1111 1111 == 255: Indicates that the UserID has all feature permissions of the room specified by roomid.
156+
* - privilegeMap == 0010 1010 == 42: Indicates that the UserID has only the permissions to enter the room and receive audio/video data.
157+
* @return usersig - Generate signature with userbuf
158+
*/
94159
public String genPrivateMapKeyWithStringRoomID(String userid, long expire, String roomstr, long privilegeMap) {
95160
byte[] userbuf = genUserBuf(userid, 0, expire, privilegeMap, 0, roomstr); //生成userbuf
96161
return genUserSig(userid, expire, userbuf);
@@ -160,6 +225,18 @@ public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
160225
dwPrivilegeMap unsigned int/4 权限位,主播0xff,观众0xab
161226
dwAccountType unsigned int/4 第三方帐号类型
162227
*/
228+
229+
//The fields required for the video check digit are placed in buf according to the network byte order.
230+
/*
231+
cVer unsigned char/1 Version number, fill in 0
232+
wAccountLen unsigned short /2 Third party's own account length
233+
account wAccountLen Third party's own account characters
234+
dwSdkAppid unsigned int/4 sdkappid
235+
dwAuthID unsigned int/4 group number
236+
dwExpTime unsigned int/4 Expiration time , use the filled value directly
237+
dwPrivilegeMap unsigned int/4 Permission bits, host 0xff, audience 0xab
238+
dwAccountType unsigned int/4 Third-party account type
239+
*/
163240
int accountLength = account.length();
164241
int roomStrLength = RoomStr.length();
165242
int offset = 0;
@@ -192,12 +269,14 @@ public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
192269
userbuf[offset++] = (byte) (sdkappid & 0x000000FF);
193270

194271
//dwAuthId,房间号
272+
//dwAuthId, room number
195273
userbuf[offset++] = (byte) ((dwAuthID & 0xFF000000) >> 24);
196274
userbuf[offset++] = (byte) ((dwAuthID & 0x00FF0000) >> 16);
197275
userbuf[offset++] = (byte) ((dwAuthID & 0x0000FF00) >> 8);
198276
userbuf[offset++] = (byte) (dwAuthID & 0x000000FF);
199277

200278
//expire,过期时间,当前时间 + 有效期(单位:秒)
279+
//expire,Expiration time, current time + validity period (unit: seconds)
201280
long currTime = System.currentTimeMillis() / 1000;
202281
long expire = currTime + dwExpTime;
203282
userbuf[offset++] = (byte) ((expire & 0xFF000000) >> 24);
@@ -206,12 +285,14 @@ public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
206285
userbuf[offset++] = (byte) (expire & 0x000000FF);
207286

208287
//dwPrivilegeMap,权限位
288+
//dwPrivilegeMap,Permission bits
209289
userbuf[offset++] = (byte) ((dwPrivilegeMap & 0xFF000000) >> 24);
210290
userbuf[offset++] = (byte) ((dwPrivilegeMap & 0x00FF0000) >> 16);
211291
userbuf[offset++] = (byte) ((dwPrivilegeMap & 0x0000FF00) >> 8);
212292
userbuf[offset++] = (byte) (dwPrivilegeMap & 0x000000FF);
213293

214294
//dwAccountType,账户类型
295+
//dwAccountType,account type
215296
userbuf[offset++] = (byte) ((dwAccountType & 0xFF000000) >> 24);
216297
userbuf[offset++] = (byte) ((dwAccountType & 0x00FF0000) >> 16);
217298
userbuf[offset++] = (byte) ((dwAccountType & 0x0000FF00) >> 8);

0 commit comments

Comments
 (0)