123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package com.yihu.jw.utils;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.springframework.util.Base64Utils;
- import javax.crypto.Mac;
- import javax.crypto.spec.SecretKeySpec;
- import java.nio.charset.Charset;
- import java.util.Arrays;
- import java.util.zip.Deflater;
- /*
- * Module: GenerateUserSig
- *
- * Function: 用于生成测试用的 UserSig,UserSig 是腾讯云为其云服务设计的一种安全保护签名。
- * 其计算方法是对 SDKAppID、UserID 和 EXPIRETIME 进行加密,加密算法为 HMAC-SHA256。
- *
- * Attention: 请不要将如下代码发布到您的线上正式版本的 App 中,原因如下:
- *
- * 本文件中的代码虽然能够正确计算出 UserSig,但仅适合快速调通 SDK 的基本功能,不适合线上产品,
- * 这是因为客户端代码中的 SECRETKEY 很容易被反编译逆向破解,尤其是 Web 端的代码被破解的难度几乎为零。
- * 一旦您的密钥泄露,攻击者就可以计算出正确的 UserSig 来盗用您的腾讯云流量。
- *
- * 正确的做法是将 UserSig 的计算代码和加密密钥放在您的业务服务器上,然后由 App 按需向您的服务器获取实时算出的 UserSig。
- * 由于破解服务器的成本要高于破解客户端 App,所以服务器计算的方案能够更好地保护您的加密密钥。
- *
- * Reference:https://cloud.tencent.com/document/product/269/32688#Server
- */
- public class GenerateUserSig {
- /**
- * 生成 tls 票据
- *
- * @param sdkappid 应用的 appid
- * @param userId 用户 id
- * @param expire 有效期,单位是秒
- * @param userbuf 默认填写null
- * @param priKeyContent 生成 tls 票据使用的私钥内容
- * @return 如果出错,会返回为空,或者有异常打印,成功返回有效的票据
- */
- public static String GenTLSSignature(long sdkappid, String userId, long expire, byte[] userbuf, String priKeyContent) {
- if (StringUtil.isEmpty(priKeyContent)) {
- return "";
- }
- long currTime = System.currentTimeMillis() / 1000;
- JSONObject sigDoc = new JSONObject();
- try {
- 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);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- String base64UserBuf = null;
- if (null != userbuf) {
- base64UserBuf = Base64Utils.encodeToString(userbuf);
- try {
- sigDoc.put("TLS.userbuf", base64UserBuf);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- String sig = hmacsha256(sdkappid, userId, currTime, expire, priKeyContent, base64UserBuf);
- if (sig.length() == 0) {
- return "";
- }
- try {
- sigDoc.put("TLS.sig", sig);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Deflater compressor = new Deflater();
- compressor.setInput(sigDoc.toString().getBytes(Charset.forName("UTF-8")));
- compressor.finish();
- byte[] compressedBytes = new byte[2048];
- int compressedBytesLength = compressor.deflate(compressedBytes);
- compressor.end();
- return new String(base64EncodeUrl(Arrays.copyOfRange(compressedBytes, 0, compressedBytesLength)));
- }
- private static String hmacsha256(long sdkappid, String userId, long currTime, long expire, String priKeyContent, String base64Userbuf) {
- String contentToBeSigned = "TLS.identifier:" + userId + "\n"
- + "TLS.sdkappid:" + sdkappid + "\n"
- + "TLS.time:" + currTime + "\n"
- + "TLS.expire:" + expire + "\n";
- if (null != base64Userbuf) {
- contentToBeSigned += "TLS.userbuf:" + base64Userbuf + "\n";
- }
- try {
- byte[] byteKey = priKeyContent.getBytes("UTF-8");
- Mac hmac = Mac.getInstance("HmacSHA256");
- SecretKeySpec keySpec = new SecretKeySpec(byteKey, "HmacSHA256");
- hmac.init(keySpec);
- byte[] byteSig = hmac.doFinal(contentToBeSigned.getBytes("UTF-8"));
- return new String(Base64Utils.encodeToString(byteSig));
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
- private static byte[] base64EncodeUrl(byte[] input) {
- byte[] base64 = new String(Base64Utils.encodeToString(input)).getBytes();
- for (int i = 0; i < base64.length; ++i)
- switch (base64[i]) {
- case '+':
- base64[i] = '*';
- break;
- case '/':
- base64[i] = '-';
- break;
- case '=':
- base64[i] = '_';
- break;
- default:
- break;
- }
- return base64;
- }
- }
|