11package com .tencentyun ;
22
33// 使用旧版本 base64 编解码实现增强兼容性
4+
45import sun .misc .BASE64Encoder ;
56
67import java .io .UnsupportedEncodingException ;
1213
1314import javax .crypto .Mac ;
1415import javax .crypto .spec .SecretKeySpec ;
16+
1517import org .json .JSONObject ;
1618
1719public class TLSSigAPIv2 {
@@ -42,14 +44,14 @@ private String hmacsha256(String identifier, long currTime, long expire, String
4244 return "" ;
4345 } catch (NoSuchAlgorithmException e ) {
4446 return "" ;
45- } catch (InvalidKeyException e ) {
47+ } catch (InvalidKeyException e ) {
4648 return "" ;
4749 }
4850 }
4951
5052 private String genUserSig (String userid , long expire , byte [] userbuf ) {
5153
52- long currTime = System .currentTimeMillis ()/ 1000 ;
54+ long currTime = System .currentTimeMillis () / 1000 ;
5355
5456 JSONObject sigDoc = new JSONObject ();
5557 sigDoc .put ("TLS.ver" , "2.0" );
@@ -71,25 +73,28 @@ private String genUserSig(String userid, long expire, byte[] userbuf) {
7173 Deflater compressor = new Deflater ();
7274 compressor .setInput (sigDoc .toString ().getBytes (Charset .forName ("UTF-8" )));
7375 compressor .finish ();
74- byte [] compressedBytes = new byte [2048 ];
76+ byte [] compressedBytes = new byte [2048 ];
7577 int compressedBytesLength = compressor .deflate (compressedBytes );
7678 compressor .end ();
7779 return (new String (Base64URL .base64EncodeUrl (Arrays .copyOfRange (compressedBytes ,
7880 0 , compressedBytesLength )))).replaceAll ("\\ s*" , "" );
7981 }
80- /**用于生成实时音视频(TRTC)业务进房权限加密串,具体用途用法参考TRTC文档:https://cloud.tencent.com/document/product/647/32240
81- * TRTC业务进房权限加密串需使用用户定义的userbuf
82- * 生成 userbuf
83- * @param account 用户名
84- * @param dwAuthID 数字房间号
85- * @param dwExpTime 过期时间:该权限加密串的过期时间,超时时间内拿到该签名,并且发起进房间操作,时间为有效期
86- * 实际填入userBuf为:expire,过期时间,当前时间 + 有效期(单位:秒)
87- * @param dwPrivilegeMap 用户权限,255表示所有权限,主播0xff,观众0xab
88- * @param dwAccountType 用户类型,默认为0
89- * @return byte[] userbuf
90- */
91- public byte [] genUserBuf (String account ,long dwAuthID , long dwExpTime ,
92- long dwPrivilegeMap ,long dwAccountType ){
82+
83+ /**
84+ * 用于生成实时音视频(TRTC)业务进房权限加密串,具体用途用法参考TRTC文档:https://cloud.tencent.com/document/product/647/32240
85+ * TRTC业务进房权限加密串需使用用户定义的userbuf
86+ * 生成 userbuf
87+ *
88+ * @param account 用户名
89+ * @param dwAuthID 数字房间号
90+ * @param dwExpTime 过期时间:该权限加密串的过期时间,超时时间内拿到该签名,并且发起进房间操作,时间为有效期
91+ * 实际填入userBuf为:expire,过期时间,当前时间 + 有效期(单位:秒)
92+ * @param dwPrivilegeMap 用户权限,255表示所有权限,主播0xff,观众0xab
93+ * @param dwAccountType 用户类型,默认为0
94+ * @return byte[] userbuf
95+ */
96+ public byte [] genUserBuf (String account , long dwAuthID , long dwExpTime ,
97+ long dwPrivilegeMap , long dwAccountType ) {
9398 //视频校验位需要用到的字段,按照网络字节序放入buf中
9499 /*
95100 cVer unsigned char/1 版本号,填0
@@ -103,58 +108,59 @@ public byte[] genUserBuf(String account ,long dwAuthID, long dwExpTime ,
103108 */
104109 int accountLength = account .length ();
105110 int offset = 0 ;
106- byte [] userbuf = new byte [1 + 2 + accountLength + 4 + 4 + 4 + 4 + 4 ];
111+ byte [] userbuf = new byte [1 + 2 + accountLength + 4 + 4 + 4 + 4 + 4 ];
107112
108113 //cVer
109114 userbuf [offset ++] = 0 ;
110115
111116 //wAccountLen
112- userbuf [offset ++] = (byte )((accountLength & 0xFF00 ) >> 8 );
113- userbuf [offset ++] = (byte )(accountLength & 0x00FF );
117+ userbuf [offset ++] = (byte ) ((accountLength & 0xFF00 ) >> 8 );
118+ userbuf [offset ++] = (byte ) (accountLength & 0x00FF );
114119
115120 //account
116121 for (; offset < 3 + accountLength ; ++offset ) {
117- userbuf [offset ] = (byte )account .charAt (offset - 3 );
122+ userbuf [offset ] = (byte ) account .charAt (offset - 3 );
118123 }
119124
120125 //dwSdkAppid
121- userbuf [offset ++] = (byte )((sdkappid & 0xFF000000 ) >> 24 );
122- userbuf [offset ++] = (byte )((sdkappid & 0x00FF0000 ) >> 16 );
123- userbuf [offset ++] = (byte )((sdkappid & 0x0000FF00 ) >> 8 );
124- userbuf [offset ++] = (byte )(sdkappid & 0x000000FF );
126+ userbuf [offset ++] = (byte ) ((sdkappid & 0xFF000000 ) >> 24 );
127+ userbuf [offset ++] = (byte ) ((sdkappid & 0x00FF0000 ) >> 16 );
128+ userbuf [offset ++] = (byte ) ((sdkappid & 0x0000FF00 ) >> 8 );
129+ userbuf [offset ++] = (byte ) (sdkappid & 0x000000FF );
125130
126131 //dwAuthId,房间号
127- userbuf [offset ++] = (byte )((dwAuthID & 0xFF000000 ) >> 24 );
128- userbuf [offset ++] = (byte )((dwAuthID & 0x00FF0000 ) >> 16 );
129- userbuf [offset ++] = (byte )((dwAuthID & 0x0000FF00 ) >> 8 );
130- userbuf [offset ++] = (byte )(dwAuthID & 0x000000FF );
132+ userbuf [offset ++] = (byte ) ((dwAuthID & 0xFF000000 ) >> 24 );
133+ userbuf [offset ++] = (byte ) ((dwAuthID & 0x00FF0000 ) >> 16 );
134+ userbuf [offset ++] = (byte ) ((dwAuthID & 0x0000FF00 ) >> 8 );
135+ userbuf [offset ++] = (byte ) (dwAuthID & 0x000000FF );
131136
132137 //expire,过期时间,当前时间 + 有效期(单位:秒)
133- long currTime = System .currentTimeMillis ()/ 1000 ;
134- long expire = currTime + dwExpTime ;
135- userbuf [offset ++] = (byte )((expire & 0xFF000000 ) >> 24 );
136- userbuf [offset ++] = (byte )((expire & 0x00FF0000 ) >> 16 );
137- userbuf [offset ++] = (byte )((expire & 0x0000FF00 ) >> 8 );
138- userbuf [offset ++] = (byte )(expire & 0x000000FF );
138+ long currTime = System .currentTimeMillis () / 1000 ;
139+ long expire = currTime + dwExpTime ;
140+ userbuf [offset ++] = (byte ) ((expire & 0xFF000000 ) >> 24 );
141+ userbuf [offset ++] = (byte ) ((expire & 0x00FF0000 ) >> 16 );
142+ userbuf [offset ++] = (byte ) ((expire & 0x0000FF00 ) >> 8 );
143+ userbuf [offset ++] = (byte ) (expire & 0x000000FF );
139144
140145 //dwPrivilegeMap,权限位
141- userbuf [offset ++] = (byte )((dwPrivilegeMap & 0xFF000000 ) >> 24 );
142- userbuf [offset ++] = (byte )((dwPrivilegeMap & 0x00FF0000 ) >> 16 );
143- userbuf [offset ++] = (byte )((dwPrivilegeMap & 0x0000FF00 ) >> 8 );
144- userbuf [offset ++] = (byte )(dwPrivilegeMap & 0x000000FF );
146+ userbuf [offset ++] = (byte ) ((dwPrivilegeMap & 0xFF000000 ) >> 24 );
147+ userbuf [offset ++] = (byte ) ((dwPrivilegeMap & 0x00FF0000 ) >> 16 );
148+ userbuf [offset ++] = (byte ) ((dwPrivilegeMap & 0x0000FF00 ) >> 8 );
149+ userbuf [offset ++] = (byte ) (dwPrivilegeMap & 0x000000FF );
145150
146151 //dwAccountType,账户类型
147- userbuf [offset ++] = (byte )((dwAccountType & 0xFF000000 ) >> 24 );
148- userbuf [offset ++] = (byte )((dwAccountType & 0x00FF0000 ) >> 16 );
149- userbuf [offset ++] = (byte )((dwAccountType & 0x0000FF00 ) >> 8 );
150- userbuf [offset ++] = (byte )(dwAccountType & 0x000000FF );
152+ userbuf [offset ++] = (byte ) ((dwAccountType & 0xFF000000 ) >> 24 );
153+ userbuf [offset ++] = (byte ) ((dwAccountType & 0x00FF0000 ) >> 16 );
154+ userbuf [offset ++] = (byte ) ((dwAccountType & 0x0000FF00 ) >> 8 );
155+ userbuf [offset ++] = (byte ) (dwAccountType & 0x000000FF );
151156
152157 return userbuf ;
153158 }
159+
154160 /**
155- *【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
161+ * 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
156162 *
157- *【参数说明】
163+ * 【参数说明】
158164 * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
159165 * @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
160166 * @return usersig -生成的签名
@@ -164,32 +170,32 @@ public String genUserSig(String userid, long expire) {
164170 }
165171
166172 /**
167- *【功能说明】
173+ * 【功能说明】
168174 * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
169175 * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
170- * - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
171- * - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
176+ * - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
177+ * - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
172178 * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】/【应用管理】/【应用信息】中打开“启动权限密钥”开关。
173179 *
174- *【参数说明】
175- * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
176- * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
177- * @param roomid - 房间号,用于指定该 userid 可以进入的房间号
180+ * 【参数说明】
181+ * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
182+ * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
183+ * @param roomid - 房间号,用于指定该 userid 可以进入的房间号
178184 * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
179- * - 第 1 位:0000 0001 = 1,创建房间的权限
180- * - 第 2 位:0000 0010 = 2,加入房间的权限
181- * - 第 3 位:0000 0100 = 4,发送语音的权限
182- * - 第 4 位:0000 1000 = 8,接收语音的权限
183- * - 第 5 位:0001 0000 = 16,发送视频的权限
184- * - 第 6 位:0010 0000 = 32,接收视频的权限
185- * - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
186- * - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
187- * - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
188- * - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
185+ * - 第 1 位:0000 0001 = 1,创建房间的权限
186+ * - 第 2 位:0000 0010 = 2,加入房间的权限
187+ * - 第 3 位:0000 0100 = 4,发送语音的权限
188+ * - 第 4 位:0000 1000 = 8,接收语音的权限
189+ * - 第 5 位:0001 0000 = 16,发送视频的权限
190+ * - 第 6 位:0010 0000 = 32,接收视频的权限
191+ * - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
192+ * - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
193+ * - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
194+ * - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
189195 * @return usersig - 生成带userbuf的签名
190196 */
191- public String genPrivateMapKey (String userid , long expire ,long roomid , long privilegeMap ) {
192- byte [] userbuf = genUserBuf (userid ,roomid ,expire ,privilegeMap ,0 ); //生成userbuf
197+ public String genPrivateMapKey (String userid , long expire , long roomid , long privilegeMap ) {
198+ byte [] userbuf = genUserBuf (userid , roomid , expire , privilegeMap , 0 ); //生成userbuf
193199 return genUserSig (userid , expire , userbuf );
194200 }
195201}
0 commit comments