From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 28 六月 2023 17:14:51 +0800
Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc""

---
 HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/EncryptUtil.java |   69 ++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/EncryptUtil.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/EncryptUtil.java
new file mode 100644
index 0000000..b12a06e
--- /dev/null
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/EncryptUtil.java
@@ -0,0 +1,69 @@
+package com.hdl.sdk.link.core.utils;
+
+import android.text.TextUtils;
+
+import com.hdl.sdk.link.common.config.TopicConstant;
+import com.hdl.sdk.link.common.utils.ByteUtils;
+
+import com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.core.bean.LinkRequest;
+import com.hdl.sdk.link.core.config.HDLLinkConfig;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Created by hxb on 2021/12/23.
+ */
+public class EncryptUtil {
+    /**
+     * 鑾峰彇鍔犲瘑鏁版嵁
+     *
+     * @param linkRequest 璇锋眰鏁版嵁
+     * @return
+     */
+    public static byte[] getEncryBytes(LinkRequest linkRequest) throws UnsupportedEncodingException {
+        //鍒ゆ柇鏄惁闇�瑕佸姞瀵�
+        if (ifNeedEncrypt(linkRequest.getTopic(), linkRequest.isEncrypt())) {
+            //闇�瑕佸姞瀵�
+            byte[] dataBytes = AesUtil.aesEncrypt(linkRequest.getData(), HDLLinkConfig.getInstance().getLocalSecret());
+            String headString = "Topic:" + linkRequest.getTopic() + "\r\n" + "Length:" + dataBytes.length + "\r\n" + "\r\n";
+            byte[] headBytes = headString.getBytes("utf-8");
+            byte[] sendBytes = ByteUtils.concatBytes(headBytes, dataBytes);
+            return sendBytes;
+        } else {
+            return linkRequest.getSendBytes();
+        }
+    }
+
+    /**
+     * 鍔犲瘑鏁版嵁
+     *
+     * param bytes 闇�瑕佸姞瀵嗘暟鎹�
+     * @param secret 鍔犲瘑瀵嗛挜
+     * @return
+     */
+    public static byte[] encryBytes(byte []bytes,String secret) {
+        if(bytes==null|| TextUtils.isEmpty(secret)){
+            LogUtils.i("浼犲叆鏁版嵁鍐呭涓虹┖鎴栬�呭瘑閽ヤ负绌�");
+            return null;
+        }
+        return AesUtil.aesEncrypt(bytes, secret);
+    }
+
+    /**
+     * 鍒ゆ柇褰撳墠涓婚鏁版嵁鏄惁闇�瑕佸姞瀵�
+     *
+     * @param topicStr 褰撳墠涓婚
+     * @return
+     */
+    public static boolean ifNeedEncrypt(String topicStr, boolean isLocalEncrypt) {
+        //杩囨护鐩稿叧闇�瑕佸姞瀵嗙殑涓婚
+        return (!topicStr.contains(TopicConstant.GATEWAY_AUTH_BROADCAST) //缃戝叧骞挎挱鍏ョ綉鎸囦护
+                && !topicStr.contains(TopicConstant.DEIVCE_AUTH_REQUEST) //鍏ョ綉璁よ瘉
+                && !topicStr.contains(TopicConstant.GATEWAY_SEARCH) //鎼滅储缃戝叧涓婚
+                && !topicStr.contains(TopicConstant.GATEWAY_SEARCH_REPLY) //鎼滅储缃戝叧涓婚鍥炲
+                && !topicStr.equals(TopicConstant.BROADCAST)
+                && isLocalEncrypt//鍚敤鍔犲瘑鏍囧織
+        );
+    }
+}

--
Gitblit v1.8.0