Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
im-common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈永峰
im-common
Commits
c4e6a6a8
Commit
c4e6a6a8
authored
Jul 29, 2021
by
yanglbme
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: declare as static method
parent
61b76e3a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
64 deletions
+56
-64
SigUtil.java
src/main/java/com/qcloud/im/util/SigUtil.java
+56
-64
No files found.
src/main/java/com/qcloud/im/util/SigUtil.java
View file @
c4e6a6a8
...
...
@@ -19,25 +19,17 @@ import java.util.zip.Deflater;
*/
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 鉴权票据
* <p>
* 【参数说明】
*
* @param user
i
d - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param user
I
d - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
* @return user
s
ig -生成的签名
* @return user
S
ig -生成的签名
*/
public
String
genUserSig
(
String
useri
d
,
long
expire
)
{
return
genUserSig
(
useri
d
,
expire
,
null
);
public
static
String
genUserSig
(
long
sdkAppId
,
String
key
,
String
userI
d
,
long
expire
)
{
return
genUserSig
(
sdkAppId
,
key
,
userI
d
,
expire
,
null
);
}
/**
...
...
@@ -50,9 +42,9 @@ public class SigUtil {
* <p>
* 【参数说明】
*
* @param user
i
d - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param user
I
d - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
* @param room
id - 房间号,用于指定该 useri
d 可以进入的房间号
* @param room
Id - 房间号,用于指定该 userI
d 可以进入的房间号
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
* - 第 1 位:0000 0001 = 1,创建房间的权限
* - 第 2 位:0000 0010 = 2,加入房间的权限
...
...
@@ -62,13 +54,13 @@ public class SigUtil {
* - 第 6 位:0010 0000 = 32,接收视频的权限
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
* - privilegeMap == 1111 1111 == 255 代表该 user
id 在该 roomi
d 房间内的所有功能权限。
* - privilegeMap == 0010 1010 == 42 代表该 user
i
d 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
* @return user
sig - 生成带userb
uf的签名
* - privilegeMap == 1111 1111 == 255 代表该 user
Id 在该 roomI
d 房间内的所有功能权限。
* - privilegeMap == 0010 1010 == 42 代表该 user
I
d 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
* @return user
Sig - 生成带userB
uf的签名
*/
public
String
genPrivateMapKey
(
String
userid
,
long
expire
,
long
roomi
d
,
long
privilegeMap
)
{
byte
[]
user
buf
=
genUserBuf
(
userid
,
roomi
d
,
expire
,
privilegeMap
,
0
,
""
);
return
genUserSig
(
userid
,
expire
,
userb
uf
);
public
static
String
genPrivateMapKey
(
long
sdkAppId
,
String
key
,
String
userId
,
long
expire
,
long
roomI
d
,
long
privilegeMap
)
{
byte
[]
user
Buf
=
genUserBuf
(
sdkAppId
,
userId
,
roomI
d
,
expire
,
privilegeMap
,
0
,
""
);
return
genUserSig
(
sdkAppId
,
key
,
userId
,
expire
,
userB
uf
);
}
/**
...
...
@@ -81,9 +73,9 @@ public class SigUtil {
* <p>
* 【参数说明】
*
* @param user
i
d - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param user
I
d - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
* @param room
str - 字符串房间号,用于指定该 useri
d 可以进入的房间号
* @param room
Str - 字符串房间号,用于指定该 userI
d 可以进入的房间号
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
* - 第 1 位:0000 0001 = 1,创建房间的权限
* - 第 2 位:0000 0010 = 2,加入房间的权限
...
...
@@ -93,16 +85,16 @@ public class SigUtil {
* - 第 6 位:0010 0000 = 32,接收视频的权限
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
* - privilegeMap == 1111 1111 == 255 代表该 user
id 在该 roomi
d 房间内的所有功能权限。
* - privilegeMap == 0010 1010 == 42 代表该 user
i
d 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
* @return user
sig - 生成带userb
uf的签名
* - privilegeMap == 1111 1111 == 255 代表该 user
Id 在该 roomI
d 房间内的所有功能权限。
* - privilegeMap == 0010 1010 == 42 代表该 user
I
d 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
* @return user
Sig - 生成带userB
uf的签名
*/
public
String
genPrivateMapKeyWithStringRoomID
(
String
userid
,
long
expire
,
String
rooms
tr
,
long
privilegeMap
)
{
byte
[]
user
buf
=
genUserBuf
(
userid
,
0
,
expire
,
privilegeMap
,
0
,
rooms
tr
);
return
genUserSig
(
userid
,
expire
,
userb
uf
);
public
static
String
genPrivateMapKeyWithStringRoomId
(
long
sdkAppId
,
String
key
,
String
userId
,
long
expire
,
String
roomS
tr
,
long
privilegeMap
)
{
byte
[]
user
Buf
=
genUserBuf
(
sdkAppId
,
userId
,
0
,
expire
,
privilegeMap
,
0
,
roomS
tr
);
return
genUserSig
(
sdkAppId
,
key
,
userId
,
expire
,
userB
uf
);
}
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"
+
"TLS.sdkappid:"
+
sdkAppId
+
"\n"
+
"TLS.time:"
+
currTime
+
"\n"
...
...
@@ -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
;
JSONObject
sigDoc
=
new
JSONObject
();
sigDoc
.
put
(
"TLS.ver"
,
"2.0"
);
...
...
@@ -136,7 +128,7 @@ public class SigUtil {
base64UserBuf
=
Base64
.
getEncoder
().
encodeToString
(
userbuf
).
replaceAll
(
"\\s*"
,
""
);
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
)
{
return
""
;
}
...
...
@@ -151,8 +143,8 @@ public class SigUtil {
0
,
compressedBytesLength
)))).
replaceAll
(
"\\s*"
,
""
);
}
public
byte
[]
genUserBuf
(
String
account
,
long
dwAuthID
,
long
dwExpTime
,
long
dwPrivilegeMap
,
long
dwAccountType
,
String
R
oomStr
)
{
public
static
byte
[]
genUserBuf
(
long
sdkAppId
,
String
account
,
long
dwAuthID
,
long
dwExpTime
,
long
dwPrivilegeMap
,
long
dwAccountType
,
String
r
oomStr
)
{
//视频校验位需要用到的字段,按照网络字节序放入buf中
/*
cVer unsigned char/1 版本号,填0
...
...
@@ -165,73 +157,73 @@ public class SigUtil {
dwAccountType unsigned int/4 第三方帐号类型
*/
int
accountLength
=
account
.
length
();
int
roomStrLength
=
R
oomStr
.
length
();
int
roomStrLength
=
r
oomStr
.
length
();
int
offset
=
0
;
int
bufLength
=
1
+
2
+
accountLength
+
20
;
if
(
roomStrLength
>
0
)
{
bufLength
=
bufLength
+
2
+
roomStrLength
;
}
byte
[]
user
b
uf
=
new
byte
[
bufLength
];
byte
[]
user
B
uf
=
new
byte
[
bufLength
];
// cVer
if
(
roomStrLength
>
0
)
{
user
b
uf
[
offset
++]
=
1
;
user
B
uf
[
offset
++]
=
1
;
}
else
{
user
b
uf
[
offset
++]
=
0
;
user
B
uf
[
offset
++]
=
0
;
}
// wAccountLen
user
b
uf
[
offset
++]
=
(
byte
)
((
accountLength
&
0xFF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
accountLength
&
0x00FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
accountLength
&
0xFF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
accountLength
&
0x00FF
);
// account
for
(;
offset
<
3
+
accountLength
;
++
offset
)
{
user
b
uf
[
offset
]
=
(
byte
)
account
.
charAt
(
offset
-
3
);
user
B
uf
[
offset
]
=
(
byte
)
account
.
charAt
(
offset
-
3
);
}
// dwSdkAppid
user
b
uf
[
offset
++]
=
(
byte
)
((
sdkAppId
&
0xFF000000
)
>>
24
);
user
b
uf
[
offset
++]
=
(
byte
)
((
sdkAppId
&
0x00FF0000
)
>>
16
);
user
b
uf
[
offset
++]
=
(
byte
)
((
sdkAppId
&
0x0000FF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
sdkAppId
&
0x000000FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
sdkAppId
&
0xFF000000
)
>>
24
);
user
B
uf
[
offset
++]
=
(
byte
)
((
sdkAppId
&
0x00FF0000
)
>>
16
);
user
B
uf
[
offset
++]
=
(
byte
)
((
sdkAppId
&
0x0000FF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
sdkAppId
&
0x000000FF
);
// dwAuthId,房间号
user
b
uf
[
offset
++]
=
(
byte
)
((
dwAuthID
&
0xFF000000
)
>>
24
);
user
b
uf
[
offset
++]
=
(
byte
)
((
dwAuthID
&
0x00FF0000
)
>>
16
);
user
b
uf
[
offset
++]
=
(
byte
)
((
dwAuthID
&
0x0000FF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
dwAuthID
&
0x000000FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwAuthID
&
0xFF000000
)
>>
24
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwAuthID
&
0x00FF0000
)
>>
16
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwAuthID
&
0x0000FF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
dwAuthID
&
0x000000FF
);
// expire,过期时间,当前时间 + 有效期(单位:秒)
long
currTime
=
System
.
currentTimeMillis
()
/
1000
;
long
expire
=
currTime
+
dwExpTime
;
user
b
uf
[
offset
++]
=
(
byte
)
((
expire
&
0xFF000000
)
>>
24
);
user
b
uf
[
offset
++]
=
(
byte
)
((
expire
&
0x00FF0000
)
>>
16
);
user
b
uf
[
offset
++]
=
(
byte
)
((
expire
&
0x0000FF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
expire
&
0x000000FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
expire
&
0xFF000000
)
>>
24
);
user
B
uf
[
offset
++]
=
(
byte
)
((
expire
&
0x00FF0000
)
>>
16
);
user
B
uf
[
offset
++]
=
(
byte
)
((
expire
&
0x0000FF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
expire
&
0x000000FF
);
// dwPrivilegeMap,权限位
user
b
uf
[
offset
++]
=
(
byte
)
((
dwPrivilegeMap
&
0xFF000000
)
>>
24
);
user
b
uf
[
offset
++]
=
(
byte
)
((
dwPrivilegeMap
&
0x00FF0000
)
>>
16
);
user
b
uf
[
offset
++]
=
(
byte
)
((
dwPrivilegeMap
&
0x0000FF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
dwPrivilegeMap
&
0x000000FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwPrivilegeMap
&
0xFF000000
)
>>
24
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwPrivilegeMap
&
0x00FF0000
)
>>
16
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwPrivilegeMap
&
0x0000FF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
dwPrivilegeMap
&
0x000000FF
);
// dwAccountType,账户类型
user
b
uf
[
offset
++]
=
(
byte
)
((
dwAccountType
&
0xFF000000
)
>>
24
);
user
b
uf
[
offset
++]
=
(
byte
)
((
dwAccountType
&
0x00FF0000
)
>>
16
);
user
b
uf
[
offset
++]
=
(
byte
)
((
dwAccountType
&
0x0000FF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
dwAccountType
&
0x000000FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwAccountType
&
0xFF000000
)
>>
24
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwAccountType
&
0x00FF0000
)
>>
16
);
user
B
uf
[
offset
++]
=
(
byte
)
((
dwAccountType
&
0x0000FF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
dwAccountType
&
0x000000FF
);
if
(
roomStrLength
>
0
)
{
// roomStrLen
user
b
uf
[
offset
++]
=
(
byte
)
((
roomStrLength
&
0xFF00
)
>>
8
);
user
b
uf
[
offset
++]
=
(
byte
)
(
roomStrLength
&
0x00FF
);
user
B
uf
[
offset
++]
=
(
byte
)
((
roomStrLength
&
0xFF00
)
>>
8
);
user
B
uf
[
offset
++]
=
(
byte
)
(
roomStrLength
&
0x00FF
);
// roomStr
for
(;
offset
<
bufLength
;
++
offset
)
{
user
buf
[
offset
]
=
(
byte
)
R
oomStr
.
charAt
(
offset
-
(
bufLength
-
roomStrLength
));
user
Buf
[
offset
]
=
(
byte
)
r
oomStr
.
charAt
(
offset
-
(
bufLength
-
roomStrLength
));
}
}
return
user
b
uf
;
return
user
B
uf
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment