设为首页 加入收藏

TOP

腾讯云通信UserSig生成.Net实现(一)
2019-09-17 17:38:46 】 浏览:47
Tags:腾讯 通信 UserSig 生成 .Net 实现

腾讯云通信后台生成usersig只有java实现代码。以下是根据java代码转换为net实现,java版GitHub地址:https://github.com/TencentVideoCloudMLVBDev/usersig_server_source/blob/master/java/WebRTCSigApi.java。

需要使用类库:Portable.BouncyCastle,SharpZipLib,直接从nuget下载安装即可。

另外我正在实现腾讯云IM服务端Sdk .NetCore版本,github地址:https://github.com/yangxuilyx/QCloudIM.AspNetCore

 public class TlsSignature
    {
        /// <summary>
        /// 获取用户sign
        /// </summary>
        /// <param name="appid">appid</param>
        /// <param name="privateKey">私钥</param>
        /// <param name="userid">用户名</param>
        /// <param name="expire">userSig有效期,出于安全考虑建议为300秒,您可以根据您的业务场景设置其他值。</param>
        /// <returns>生成的userSig</returns>
        public static string GenUserSig(string appid, string privateKey, string userid, int expire)
        {
var time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; String serialString = "TLS.appid_at_3rd:" + 0 + "\n" + "TLS.account_type:" + 0 + "\n" + "TLS.identifier:" + userid + "\n" + "TLS.sdk_appid:" + appid + "\n" + "TLS.time:" + time + "\n" + "TLS.expire_after:" + expire + "\n"; var sign = Convert.ToBase64String(Sign(privateKey, Encoding.UTF8.GetBytes(serialString))); String jsonString = "{" + "\"TLS.account_type\":\"" + 0 + "\"," + "\"TLS.identifier\":\"" + userid + "\"," + "\"TLS.appid_at_3rd\":\"" + 0 + "\"," + "\"TLS.sdk_appid\":\"" + appid + "\"," + "\"TLS.expire_after\":\"" + expire + "\"," + "\"TLS.sig\":\"" + sign + "\"," + "\"TLS.time\":\"" + time + "\"," + "\"TLS.version\": \"201512300000\"" + "}"; var compressBytes = Compress(Encoding.UTF8.GetBytes(jsonString)); var userSign = Base64UrlEncode(compressBytes); return userSign; } private static byte[] Compress(byte[] data) { Deflater mDeflater = new Deflater(); mDeflater.SetInput(data); mDeflater.Finish(); byte[] compressBytes = new byte[512]; int compressBytesLength = mDeflater.Deflate(compressBytes); mDeflater.Flush(); return compressBytes.Take(compressBytesLength).ToArray(); } /// <summary> /// ECDSA-SHA256签名 /// </summary> /// <param name="privateKey">私钥</param> /// <param name="data">需要签名的数据</param> /// <returns></returns> private static byte[] Sign(string privateKey, byte[] data) { ECPrivateKeyParameters privateKeyParam = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); var signer = SignerUtilities.GetSigner("SHA256withECDSA"); signer.Init(true, privateKeyParam); signer.BlockUpdate(data, 0, data.Length); return signer.GenerateSignature(); } /// <summary> /// 验证ECDSA-SHA
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.net Core 部署到 Linux 下一篇VS2017 IIS 部署.net core web项目

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目