Commit c4e6a6a8 authored by yanglbme's avatar yanglbme

feat: declare as static method

parent 61b76e3a
...@@ -19,25 +19,17 @@ import java.util.zip.Deflater; ...@@ -19,25 +19,17 @@ import java.util.zip.Deflater;
*/ */
public class SigUtil { public class SigUtil {
private final long sdkAppId;
private final String key;
public SigUtil(long sdkAppId, String key) {
this.sdkAppId = sdkAppId;
this.key = key;
}
/** /**
* 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据 * 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
* <p> * <p>
* 【参数说明】 * 【参数说明】
* *
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。 * @param userId - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。 * @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
* @return usersig -生成的签名 * @return userSig -生成的签名
*/ */
public String genUserSig(String userid, long expire) { public static String genUserSig(long sdkAppId, String key, String userId, long expire) {
return genUserSig(userid, expire, null); return genUserSig(sdkAppId, key, userId, expire, null);
} }
/** /**
...@@ -50,9 +42,9 @@ public class SigUtil { ...@@ -50,9 +42,9 @@ public class SigUtil {
* <p> * <p>
* 【参数说明】 * 【参数说明】
* *
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。 * @param userId - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。 * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
* @param roomid - 房间号,用于指定该 userid 可以进入的房间号 * @param roomId - 房间号,用于指定该 userId 可以进入的房间号
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关: * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
* - 第 1 位:0000 0001 = 1,创建房间的权限 * - 第 1 位:0000 0001 = 1,创建房间的权限
* - 第 2 位:0000 0010 = 2,加入房间的权限 * - 第 2 位:0000 0010 = 2,加入房间的权限
...@@ -62,13 +54,13 @@ public class SigUtil { ...@@ -62,13 +54,13 @@ public class SigUtil {
* - 第 6 位:0010 0000 = 32,接收视频的权限 * - 第 6 位:0010 0000 = 32,接收视频的权限
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限 * - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限 * - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。 * - privilegeMap == 1111 1111 == 255 代表该 userId 在该 roomId 房间内的所有功能权限。
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。 * - privilegeMap == 0010 1010 == 42 代表该 userId 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
* @return usersig - 生成带userbuf的签名 * @return userSig - 生成带userBuf的签名
*/ */
public String genPrivateMapKey(String userid, long expire, long roomid, long privilegeMap) { public static String genPrivateMapKey(long sdkAppId, String key, String userId, long expire, long roomId, long privilegeMap) {
byte[] userbuf = genUserBuf(userid, roomid, expire, privilegeMap, 0, ""); byte[] userBuf = genUserBuf(sdkAppId, userId, roomId, expire, privilegeMap, 0, "");
return genUserSig(userid, expire, userbuf); return genUserSig(sdkAppId, key, userId, expire, userBuf);
} }
/** /**
...@@ -81,9 +73,9 @@ public class SigUtil { ...@@ -81,9 +73,9 @@ public class SigUtil {
* <p> * <p>
* 【参数说明】 * 【参数说明】
* *
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。 * @param userId - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。 * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
* @param roomstr - 字符串房间号,用于指定该 userid 可以进入的房间号 * @param roomStr - 字符串房间号,用于指定该 userId 可以进入的房间号
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关: * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
* - 第 1 位:0000 0001 = 1,创建房间的权限 * - 第 1 位:0000 0001 = 1,创建房间的权限
* - 第 2 位:0000 0010 = 2,加入房间的权限 * - 第 2 位:0000 0010 = 2,加入房间的权限
...@@ -93,16 +85,16 @@ public class SigUtil { ...@@ -93,16 +85,16 @@ public class SigUtil {
* - 第 6 位:0010 0000 = 32,接收视频的权限 * - 第 6 位:0010 0000 = 32,接收视频的权限
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限 * - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限 * - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。 * - privilegeMap == 1111 1111 == 255 代表该 userId 在该 roomId 房间内的所有功能权限。
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。 * - privilegeMap == 0010 1010 == 42 代表该 userId 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
* @return usersig - 生成带userbuf的签名 * @return userSig - 生成带userBuf的签名
*/ */
public String genPrivateMapKeyWithStringRoomID(String userid, long expire, String roomstr, long privilegeMap) { public static String genPrivateMapKeyWithStringRoomId(long sdkAppId, String key, String userId, long expire, String roomStr, long privilegeMap) {
byte[] userbuf = genUserBuf(userid, 0, expire, privilegeMap, 0, roomstr); byte[] userBuf = genUserBuf(sdkAppId, userId, 0, expire, privilegeMap, 0, roomStr);
return genUserSig(userid, expire, userbuf); return genUserSig(sdkAppId, key, userId, expire, userBuf);
} }
private String hmacsha256(String identifier, long currTime, long expire, String base64Userbuf) { private static String hmacsha256(long sdkAppId, String key, String identifier, long currTime, long expire, String base64Userbuf) {
String contentToBeSigned = "TLS.identifier:" + identifier + "\n" String contentToBeSigned = "TLS.identifier:" + identifier + "\n"
+ "TLS.sdkappid:" + sdkAppId + "\n" + "TLS.sdkappid:" + sdkAppId + "\n"
+ "TLS.time:" + currTime + "\n" + "TLS.time:" + currTime + "\n"
...@@ -122,7 +114,7 @@ public class SigUtil { ...@@ -122,7 +114,7 @@ public class SigUtil {
} }
} }
private String genUserSig(String userid, long expire, byte[] userbuf) { private static String genUserSig(long sdkAppId, String key, String userid, long expire, byte[] userbuf) {
long currTime = System.currentTimeMillis() / 1000; long currTime = System.currentTimeMillis() / 1000;
JSONObject sigDoc = new JSONObject(); JSONObject sigDoc = new JSONObject();
sigDoc.put("TLS.ver", "2.0"); sigDoc.put("TLS.ver", "2.0");
...@@ -136,7 +128,7 @@ public class SigUtil { ...@@ -136,7 +128,7 @@ public class SigUtil {
base64UserBuf = Base64.getEncoder().encodeToString(userbuf).replaceAll("\\s*", ""); base64UserBuf = Base64.getEncoder().encodeToString(userbuf).replaceAll("\\s*", "");
sigDoc.put("TLS.userbuf", base64UserBuf); sigDoc.put("TLS.userbuf", base64UserBuf);
} }
String sig = hmacsha256(userid, currTime, expire, base64UserBuf); String sig = hmacsha256(sdkAppId, key, userid, currTime, expire, base64UserBuf);
if (sig.length() == 0) { if (sig.length() == 0) {
return ""; return "";
} }
...@@ -151,8 +143,8 @@ public class SigUtil { ...@@ -151,8 +143,8 @@ public class SigUtil {
0, compressedBytesLength)))).replaceAll("\\s*", ""); 0, compressedBytesLength)))).replaceAll("\\s*", "");
} }
public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime, public static byte[] genUserBuf(long sdkAppId, String account, long dwAuthID, long dwExpTime,
long dwPrivilegeMap, long dwAccountType, String RoomStr) { long dwPrivilegeMap, long dwAccountType, String roomStr) {
//视频校验位需要用到的字段,按照网络字节序放入buf中 //视频校验位需要用到的字段,按照网络字节序放入buf中
/* /*
cVer unsigned char/1 版本号,填0 cVer unsigned char/1 版本号,填0
...@@ -165,73 +157,73 @@ public class SigUtil { ...@@ -165,73 +157,73 @@ public class SigUtil {
dwAccountType unsigned int/4 第三方帐号类型 dwAccountType unsigned int/4 第三方帐号类型
*/ */
int accountLength = account.length(); int accountLength = account.length();
int roomStrLength = RoomStr.length(); int roomStrLength = roomStr.length();
int offset = 0; int offset = 0;
int bufLength = 1 + 2 + accountLength + 20; int bufLength = 1 + 2 + accountLength + 20;
if (roomStrLength > 0) { if (roomStrLength > 0) {
bufLength = bufLength + 2 + roomStrLength; bufLength = bufLength + 2 + roomStrLength;
} }
byte[] userbuf = new byte[bufLength]; byte[] userBuf = new byte[bufLength];
// cVer // cVer
if (roomStrLength > 0) { if (roomStrLength > 0) {
userbuf[offset++] = 1; userBuf[offset++] = 1;
} else { } else {
userbuf[offset++] = 0; userBuf[offset++] = 0;
} }
// wAccountLen // wAccountLen
userbuf[offset++] = (byte) ((accountLength & 0xFF00) >> 8); userBuf[offset++] = (byte) ((accountLength & 0xFF00) >> 8);
userbuf[offset++] = (byte) (accountLength & 0x00FF); userBuf[offset++] = (byte) (accountLength & 0x00FF);
// account // account
for (; offset < 3 + accountLength; ++offset) { for (; offset < 3 + accountLength; ++offset) {
userbuf[offset] = (byte) account.charAt(offset - 3); userBuf[offset] = (byte) account.charAt(offset - 3);
} }
// dwSdkAppid // dwSdkAppid
userbuf[offset++] = (byte) ((sdkAppId & 0xFF000000) >> 24); userBuf[offset++] = (byte) ((sdkAppId & 0xFF000000) >> 24);
userbuf[offset++] = (byte) ((sdkAppId & 0x00FF0000) >> 16); userBuf[offset++] = (byte) ((sdkAppId & 0x00FF0000) >> 16);
userbuf[offset++] = (byte) ((sdkAppId & 0x0000FF00) >> 8); userBuf[offset++] = (byte) ((sdkAppId & 0x0000FF00) >> 8);
userbuf[offset++] = (byte) (sdkAppId & 0x000000FF); userBuf[offset++] = (byte) (sdkAppId & 0x000000FF);
// dwAuthId,房间号 // dwAuthId,房间号
userbuf[offset++] = (byte) ((dwAuthID & 0xFF000000) >> 24); userBuf[offset++] = (byte) ((dwAuthID & 0xFF000000) >> 24);
userbuf[offset++] = (byte) ((dwAuthID & 0x00FF0000) >> 16); userBuf[offset++] = (byte) ((dwAuthID & 0x00FF0000) >> 16);
userbuf[offset++] = (byte) ((dwAuthID & 0x0000FF00) >> 8); userBuf[offset++] = (byte) ((dwAuthID & 0x0000FF00) >> 8);
userbuf[offset++] = (byte) (dwAuthID & 0x000000FF); userBuf[offset++] = (byte) (dwAuthID & 0x000000FF);
// expire,过期时间,当前时间 + 有效期(单位:秒) // expire,过期时间,当前时间 + 有效期(单位:秒)
long currTime = System.currentTimeMillis() / 1000; long currTime = System.currentTimeMillis() / 1000;
long expire = currTime + dwExpTime; long expire = currTime + dwExpTime;
userbuf[offset++] = (byte) ((expire & 0xFF000000) >> 24); userBuf[offset++] = (byte) ((expire & 0xFF000000) >> 24);
userbuf[offset++] = (byte) ((expire & 0x00FF0000) >> 16); userBuf[offset++] = (byte) ((expire & 0x00FF0000) >> 16);
userbuf[offset++] = (byte) ((expire & 0x0000FF00) >> 8); userBuf[offset++] = (byte) ((expire & 0x0000FF00) >> 8);
userbuf[offset++] = (byte) (expire & 0x000000FF); userBuf[offset++] = (byte) (expire & 0x000000FF);
// dwPrivilegeMap,权限位 // dwPrivilegeMap,权限位
userbuf[offset++] = (byte) ((dwPrivilegeMap & 0xFF000000) >> 24); userBuf[offset++] = (byte) ((dwPrivilegeMap & 0xFF000000) >> 24);
userbuf[offset++] = (byte) ((dwPrivilegeMap & 0x00FF0000) >> 16); userBuf[offset++] = (byte) ((dwPrivilegeMap & 0x00FF0000) >> 16);
userbuf[offset++] = (byte) ((dwPrivilegeMap & 0x0000FF00) >> 8); userBuf[offset++] = (byte) ((dwPrivilegeMap & 0x0000FF00) >> 8);
userbuf[offset++] = (byte) (dwPrivilegeMap & 0x000000FF); userBuf[offset++] = (byte) (dwPrivilegeMap & 0x000000FF);
// dwAccountType,账户类型 // dwAccountType,账户类型
userbuf[offset++] = (byte) ((dwAccountType & 0xFF000000) >> 24); userBuf[offset++] = (byte) ((dwAccountType & 0xFF000000) >> 24);
userbuf[offset++] = (byte) ((dwAccountType & 0x00FF0000) >> 16); userBuf[offset++] = (byte) ((dwAccountType & 0x00FF0000) >> 16);
userbuf[offset++] = (byte) ((dwAccountType & 0x0000FF00) >> 8); userBuf[offset++] = (byte) ((dwAccountType & 0x0000FF00) >> 8);
userbuf[offset++] = (byte) (dwAccountType & 0x000000FF); userBuf[offset++] = (byte) (dwAccountType & 0x000000FF);
if (roomStrLength > 0) { if (roomStrLength > 0) {
// roomStrLen // roomStrLen
userbuf[offset++] = (byte) ((roomStrLength & 0xFF00) >> 8); userBuf[offset++] = (byte) ((roomStrLength & 0xFF00) >> 8);
userbuf[offset++] = (byte) (roomStrLength & 0x00FF); userBuf[offset++] = (byte) (roomStrLength & 0x00FF);
// roomStr // roomStr
for (; offset < bufLength; ++offset) { for (; offset < bufLength; ++offset) {
userbuf[offset] = (byte) RoomStr.charAt(offset - (bufLength - roomStrLength)); userBuf[offset] = (byte) roomStr.charAt(offset - (bufLength - roomStrLength));
} }
} }
return userbuf; return userBuf;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment