腾讯云IM即时通讯

获取UserSig工具类

import com.alibaba.fastjson.JSONObject;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.security.*;
import java.util.Arrays;
import java.util.zip.Deflater;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


public class TLSSigAPIv2Util {

    final private long sdkappid;
    final private String key;

    public TLSSigAPIv2Util(long sdkappid, String key) {
        this.sdkappid = sdkappid;
        this.key = key;
    }

    /**
     * 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
     * <p>
     * 【参数说明】
     *
     * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
     * @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
     * @return usersig -生成的签名
     */
    public String genUserSig(String userid, long expire) {
        return genUserSig(userid, expire, null);
    }

    /**
     * 【功能说明】
     * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
     * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
     * - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
     * - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
     * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】/【应用管理】/【应用信息】中打开“启动权限密钥”开关。
     * <p>
     * 【参数说明】
     *
     * @param userid       - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
     * @param expire       - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
     * @param roomid       - 房间号,用于指定该 userid 可以进入的房间号
     * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
     *                     - 第 1 位:0000 0001 = 1,创建房间的权限
     *                     - 第 2 位:0000 0010 = 2,加入房间的权限
     *                     - 第 3 位:0000 0100 = 4,发送语音的权限
     *                     - 第 4 位:0000 1000 = 8,接收语音的权限
     *                     - 第 5 位:0001 0000 = 16,发送视频的权限
     *                     - 第 6 位:0010 0000 = 32,接收视频的权限
     *                     - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
     *                     - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
     *                     - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
     *                     - privilegeMap == 0010 1010 == 42  代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
     * @return usersig - 生成带userbuf的签名
     */
    public String genPrivateMapKey(String userid, long expire, long roomid, long privilegeMap) {
        byte[] userbuf = genUserBuf(userid, roomid, expire, privilegeMap, 0, "");  //生成userbuf
        return genUserSig(userid, expire, userbuf);
    }

    /**
     * 【功能说明】
     * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
     * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
     * - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
     * - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
     * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】/【应用管理】/【应用信息】中打开“启动权限密钥”开关。
     * <p>
     * 【参数说明】
     *
     * @param userid       - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
     * @param expire       - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
     * @param roomstr      - 字符串房间号,用于指定该 userid 可以进入的房间号
     * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
     *                     - 第 1 位:0000 0001 = 1,创建房间的权限
     *                     - 第 2 位:0000 0010 = 2,加入房间的权限
     *                     - 第 3 位:0000 0100 = 4,发送语音的权限
     *                     - 第 4 位:0000 1000 = 8,接收语音的权限
     *                     - 第 5 位:0001 0000 = 16,发送视频的权限
     *                     - 第 6 位:0010 0000 = 32,接收视频的权限
     *                     - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
     *                     - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
     *                     - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
     *                     - privilegeMap == 0010 1010 == 42  代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
     * @return usersig - 生成带userbuf的签名
     */
    public String genPrivateMapKeyWithStringRoomID(String userid, long expire, String roomstr, long privilegeMap) {
        byte[] userbuf = genUserBuf(userid, 0, expire, privilegeMap, 0, roomstr);  //生成userbuf
        return genUserSig(userid, expire, userbuf);
    }

    private String hmacsha256(String identifier, long currTime, long expire, String base64Userbuf) {
        String contentToBeSigned = "TLS.identifier:" + identifier + "\n"
                + "TLS.sdkappid:" + sdkappid + "\n"
                + "TLS.time:" + currTime + "\n"
                + "TLS.expire:" + expire + "\n";
        if (null != base64Userbuf) {
            contentToBeSigned += "TLS.userbuf:" + base64Userbuf + "\n";
        }
        try {
            byte[] byteKey = key.getBytes(StandardCharsets.UTF_8);
            Mac hmac = Mac.getInstance("HmacSHA256");
            SecretKeySpec keySpec = new SecretKeySpec(byteKey, "HmacSHA256");
            hmac.init(keySpec);
            byte[] byteSig = hmac.doFinal(contentToBeSigned.getBytes(StandardCharsets.UTF_8));
            return (Base64.getEncoder().encodeToString(byteSig)).replaceAll("\\s*", "");
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            return "";
        }
    }

    private String genUserSig(String userid, long expire, byte[] userbuf) {

        long currTime = System.currentTimeMillis() / 1000;

        JSONObject sigDoc = new JSONObject();
        sigDoc.put("TLS.ver", "2.0");
        sigDoc.put("TLS.identifier", userid);
        sigDoc.put("TLS.sdkappid", sdkappid);
        sigDoc.put("TLS.expire", expire);
        sigDoc.put("TLS.time", currTime);

        String base64UserBuf = null;
        if (null != userbuf) {
            base64UserBuf = Base64.getEncoder().encodeToString(userbuf).replaceAll("\\s*", "");
            sigDoc.put("TLS.userbuf", base64UserBuf);
        }
        String sig = hmacsha256(userid, currTime, expire, base64UserBuf);
        if (sig.length() == 0) {
            return "";
        }
        sigDoc.put("TLS.sig", sig);
        Deflater compressor = new Deflater();
        compressor.setInput(sigDoc.toString().getBytes(StandardCharsets.UTF_8));
        compressor.finish();
        byte[] compressedBytes = new byte[2048];
        int compressedBytesLength = compressor.deflate(compressedBytes);
        compressor.end();
        return (new String(Base64URL.base64EncodeUrl(Arrays.copyOfRange(compressedBytes,
                0, compressedBytesLength)))).replaceAll("\\s*", "");
    }

    public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
                             long dwPrivilegeMap, long dwAccountType, String RoomStr) {
        //视频校验位需要用到的字段,按照网络字节序放入buf中
        /*
         cVer    unsigned char/1 版本号,填0
         wAccountLen unsigned short /2   第三方自己的帐号长度
         account wAccountLen 第三方自己的帐号字符
         dwSdkAppid  unsigned int/4  sdkappid
         dwAuthID    unsigned int/4  群组号码
         dwExpTime   unsigned int/4  过期时间 ,直接使用填入的值
         dwPrivilegeMap  unsigned int/4  权限位,主播0xff,观众0xab
         dwAccountType   unsigned int/4  第三方帐号类型
         */
        int accountLength = account.length();
        int roomStrLength = RoomStr.length();
        int offset = 0;
        int bufLength = 1 + 2 + accountLength + 20 ;
        if (roomStrLength > 0) {
            bufLength = bufLength + 2 + roomStrLength;
        }
        byte[] userbuf = new byte[bufLength];

        //cVer
        if (roomStrLength > 0) {
            userbuf[offset++] = 1;
        } else {
            userbuf[offset++] = 0;
        }

        //wAccountLen
        userbuf[offset++] = (byte) ((accountLength & 0xFF00) >> 8);
        userbuf[offset++] = (byte) (accountLength & 0x00FF);

        //account
        for (; offset < 3 + accountLength; ++offset) {
            userbuf[offset] = (byte) account.charAt(offset - 3);
        }

        //dwSdkAppid
        userbuf[offset++] = (byte) ((sdkappid & 0xFF000000) >> 24);
        userbuf[offset++] = (byte) ((sdkappid & 0x00FF0000) >> 16);
        userbuf[offset++] = (byte) ((sdkappid & 0x0000FF00) >> 8);
        userbuf[offset++] = (byte) (sdkappid & 0x000000FF);

        //dwAuthId,房间号
        userbuf[offset++] = (byte) ((dwAuthID & 0xFF000000) >> 24);
        userbuf[offset++] = (byte) ((dwAuthID & 0x00FF0000) >> 16);
        userbuf[offset++] = (byte) ((dwAuthID & 0x0000FF00) >> 8);
        userbuf[offset++] = (byte) (dwAuthID & 0x000000FF);

        //expire,过期时间,当前时间 + 有效期(单位:秒)
        long currTime = System.currentTimeMillis() / 1000;
        long expire = currTime + dwExpTime;
        userbuf[offset++] = (byte) ((expire & 0xFF000000) >> 24);
        userbuf[offset++] = (byte) ((expire & 0x00FF0000) >> 16);
        userbuf[offset++] = (byte) ((expire & 0x0000FF00) >> 8);
        userbuf[offset++] = (byte) (expire & 0x000000FF);

        //dwPrivilegeMap,权限位
        userbuf[offset++] = (byte) ((dwPrivilegeMap & 0xFF000000) >> 24);
        userbuf[offset++] = (byte) ((dwPrivilegeMap & 0x00FF0000) >> 16);
        userbuf[offset++] = (byte) ((dwPrivilegeMap & 0x0000FF00) >> 8);
        userbuf[offset++] = (byte) (dwPrivilegeMap & 0x000000FF);

        //dwAccountType,账户类型
        userbuf[offset++] = (byte) ((dwAccountType & 0xFF000000) >> 24);
        userbuf[offset++] = (byte) ((dwAccountType & 0x00FF0000) >> 16);
        userbuf[offset++] = (byte) ((dwAccountType & 0x0000FF00) >> 8);
        userbuf[offset++] = (byte) (dwAccountType & 0x000000FF);


        if (roomStrLength > 0) {
            //roomStrLen
            userbuf[offset++] = (byte) ((roomStrLength & 0xFF00) >> 8);
            userbuf[offset++] = (byte) (roomStrLength & 0x00FF);

            //roomStr
            for (; offset < bufLength; ++offset) {
                userbuf[offset] = (byte) RoomStr.charAt(offset - (bufLength - roomStrLength));
            }
        }
        return userbuf;
    }
}
KakaopayAmt": "", "IsBlue": "", "BlueResultNo": "", "BlueAmt": "", "IsAsiana": "", "AsianaResultNo": "", "AsianaAmt": "", "IsSmilecard": "", 腾讯云IM提供了适用于 Vue.js 的即时通讯 SDK,可以方便地在 Vue.js 项目中 "SmilecardResultNo": "", "SmilecardAmt": "", "IsHanaskcard": "", "Hanaskcard使用。使用前需要先引入 SDK,并且需要在腾讯云 IM 控制台上创建应用并获取 SDKAppIDResultNo": "", "HanaskcardAmt": "", "IsHanaone": "", "HanaoneResultNo": "", 和密钥信息。 以下是使用腾讯云 IM SDK 在 Vue.js 中实现简单的即时通讯功能的示 "HanaoneAmt": "", "IsLotte": "", "LotteResultNo": "", "LotteAmt":例代码: ```html <template> <div> <div v-for="msg in msgList" :key="msg.time "", "IsHyundaione": "", "HyundaioneResultNo": "", "HyundaioneAmt": "", ""> <p>{{ msg.sender }}: {{ msg.content }}</p> <small>{{ msg.time }}</small> </div> IsNonghyupone": "", "NonghyuponeResultNo": "", "NonghyuponeAmt": "", <input v-model="inputMsg" @keydown.enter="sendMsg" /> </div> </template> <script> import TIM from ' "IsSamsungone": "", "SamsungoneResultNo": "", "SamsungoneAmt": "", "IsWoorionetim-js-sdk' export default { data() { return { tim: null, userID: 'user1', convID": "", "WoorioneResultNo": "", "WoorioneAmt": "", "IsShinhancardone": "", : 'conv1', inputMsg: '', msgList: [] } }, mounted() { this.initIM() "ShinhancardoneResultNo": "", "ShinhancardoneAmt": "", "IsHyundaicardone }, methods: { initIM() { this.tim = TIM.create({ SDKAppID: 'xxx', // 替换为": "", "HyundaicardoneResultNo": "", "HyundaicardoneAmt": "", "IsSamsun自己的 SDKAppID userID: this.userID, userSig: 'xxx' // 替换为自己的密钥ghappyone": "", "SamsunghappyoneResultNo": "", "SamsunghappyoneAmt": "", 信息 }) this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, this.handleMsgReceived) this.tim.login() }, sendMsg() "IsNonghyupcardone": "", "NonghyupcardoneResultNo": "", "Nonghyupcard { const msg = this.tim.createTextMessage({ to: this.convID, payload: { text: this.inputMsg } }) this.tim.sendMessage(msg) this.msgList.push({ sender: this.userID, content: this.inputMsg, oneAmt": "", "IsHappymoneycardone": "", "HappymoneycardoneResultNo": "", " time: new Date().toLocaleString() }) this.inputMsg = '' }, handleMsgReceived(event) { const msgHappymoneycardoneAmt": "", "IsCulturesurvivalone": "", "CulturesurvivaloneResult = event.data[0] this.msgList.push({ sender: msg.from, content: msg.payload.text, time: newNo": "", "CulturesurvivaloneAmt": "", "IsDongaone": "", "DongaoneResultNo Date(msg.time * 1000).toLocaleString() }) } } } </script> ``` 这个示例中,": "", "DongaoneAmt": "", "IsGmarketone": "", "GmarketoneResultNo": "", 我们使用了 tim-js-sdk 库来操作腾讯云 IM,通过创建 TIM 实例来进行登录、发送消息、接收消息 "GmarketoneAmt": "", "IsEtcCardone": "", "EtcCardoneResultNo": "", "EtcCardoneAmt": "", "IsBlueone": "", "BlueoneResultNo": "", "BlueoneAmt": "", 等操作。在模板中,我们使用 v-for 指令来展示消息列表,并通过 v-model 指令来实现输入框的双向绑定,通过 @keydown.enter 事件监听来发送消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微微一笑满城空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值