From 44155b50cbb4f6ad78474f40331ed8838a3b0d49 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 15 十一月 2021 09:27:36 +0800
Subject: [PATCH] 2021-11-15 1.增加认证和加密通讯,未调通

---
 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java |   65 ++++++++++++++++++++++++++++++++
 1 files changed, 64 insertions(+), 1 deletions(-)

diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java
index 3634ce4..29b1941 100644
--- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java
+++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java
@@ -1,8 +1,18 @@
 package com.hdl.sdk.connect.bean;
 
 import android.text.TextUtils;
+import android.util.Log;
 
 import androidx.annotation.NonNull;
+
+import com.hdl.sdk.common.config.AuthenticateConfig;
+import com.hdl.sdk.connect.utils.AESUtils;
+import com.hdl.sdk.connect.utils.AesUtil;
+
+import java.io.UnsupportedEncodingException;
+
+import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
+import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH;
 
 /**
  * Created by Tong on 2021/9/29.
@@ -10,6 +20,7 @@
 public class LinkRequest {
     private String topic;
     private String data;
+
     private int length;
 
     public LinkRequest() {
@@ -35,7 +46,8 @@
     public void setData(String data) {
         this.data = data;
         if (!TextUtils.isEmpty(data)) {
-            setLength(data.length());
+            setLength(getBytesLength(data));
+//            setLength(data.length());
         } else {
             setLength(0);
         }
@@ -61,4 +73,55 @@
                 "\r\n\r\n" +
                 getData();
     }
+
+    private static byte[] stringToBytes(String str) {
+        try {
+            // 浣跨敤鎸囧畾鐨勫瓧绗﹂泦灏嗘瀛楃涓茬紪鐮佷负byte搴忓垪骞跺瓨鍒颁竴涓猙yte鏁扮粍涓�
+            return str.getBytes("utf-8");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        return new byte[]{};
+    }
+
+    private int getBytesLength(String str){
+        return stringToBytes(str).length;
+    }
+
+    /**
+     * 鍚堝苟鏁扮粍
+     * @param bt1
+     * @param bt2
+     * @return
+     */
+    public static byte[] byteMerger(byte[] bt1, byte[] bt2){
+        byte[] bt3 = new byte[bt1.length + bt2.length];
+        System.arraycopy(bt1, 0, bt3, 0, bt1.length);
+        System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
+        return bt3;
+    }
+    /**
+     * 鑾峰彇鍙戦�佹暟鎹産yte
+     *
+     * @return
+     */
+    public byte[] getSendBytes() {
+        try {
+            //鍒ゆ柇鏄惁闇�瑕佸姞瀵�
+            if (AuthenticateConfig.getInstance().ifNeedEncrypt(topic)) {
+                //闇�瑕佸姞瀵�
+                byte[] dataBytes = AesUtil.aesEncrypt(stringToBytes(data), AuthenticateConfig.getInstance().getLocalSecret());
+                String headString = "Topic:" + getTopic() + "\r\n" + "Length:" + dataBytes.length + "\r\n" + "\r\n";
+//                byte[] headBytes = headString.getBytes("utf-8");
+                byte[] headBytes = headString.getBytes();
+                byte[] sendBytes = byteMerger(headBytes, dataBytes);
+                return sendBytes;
+            } else {
+                return this.toString().getBytes("utf-8");
+            }
+
+        } catch (Exception e) {
+            return new byte[]{};
+        }
+    }
 }

--
Gitblit v1.8.0