This is an automated email from the ASF dual-hosted git repository.

zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new d0074fb4ee2 Add HexStringUtil (#34082)
d0074fb4ee2 is described below

commit d0074fb4ee294828a164b58bb7ec9f712e7b4b73
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Dec 16 23:44:38 2024 +0800

    Add HexStringUtil (#34082)
    
    * Add HexStringUtil
    
    * Add HexStringUtil
---
 .../authentication/OpenGaussMacCalculator.java     | 15 +------
 .../infra/util/string/HexStringUtil.java           | 46 ++++++++++++++++++++++
 .../infra/util/string/HexStringUtilTest.java       | 33 ++++++++++++++++
 3 files changed, 81 insertions(+), 13 deletions(-)

diff --git 
a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussMacCalculator.java
 
b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussMacCalculator.java
index 91c75b05a95..78aa7961c86 100644
--- 
a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussMacCalculator.java
+++ 
b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussMacCalculator.java
@@ -23,6 +23,7 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.util.string.HexStringUtil;
 
 import javax.crypto.Mac;
 import javax.crypto.SecretKeyFactory;
@@ -58,7 +59,7 @@ public final class OpenGaussMacCalculator {
     public static String requestServerMac(final String password, final 
OpenGaussAuthenticationHexData authHexData, final int serverIteration) {
         byte[] serverKey = getMacResult(generateSecretKey(password, 
authHexData.getSalt(), serverIteration), 
MacType.SERVER.data.getBytes(StandardCharsets.UTF_8));
         byte[] result = getMacResult(serverKey, 
toHexBytes(authHexData.getNonce()));
-        return toHexString(result);
+        return HexStringUtil.toHexString(result);
     }
     
     /**
@@ -118,18 +119,6 @@ public final class OpenGaussMacCalculator {
         return (byte) "0123456789ABCDEF".indexOf(c);
     }
     
-    private static String toHexString(final byte[] hexBytes) {
-        StringBuilder result = new StringBuilder();
-        for (byte each : hexBytes) {
-            String hex = Integer.toHexString(each & 255);
-            if (hex.length() < 2) {
-                result.append(0);
-            }
-            result.append(hex);
-        }
-        return result.toString();
-    }
-    
     private static byte[] xor(final byte[] password1, final byte[] password2) {
         Preconditions.checkArgument(password1.length == password2.length, "Xor 
values with different length.");
         int length = password1.length;
diff --git 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/string/HexStringUtil.java
 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/string/HexStringUtil.java
new file mode 100644
index 00000000000..bdd7aed7e93
--- /dev/null
+++ 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/string/HexStringUtil.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.util.string;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * Hex string utility class.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class HexStringUtil {
+    
+    /**
+     * Convert byte array to hex string.
+     *
+     * @param bytes bytes
+     * @return hex string
+     */
+    public static String toHexString(final byte[] bytes) {
+        StringBuilder result = new StringBuilder();
+        for (byte each : bytes) {
+            String hex = Integer.toHexString(0xff & each);
+            if (1 == hex.length()) {
+                result.append('0');
+            }
+            result.append(hex);
+        }
+        return result.toString();
+    }
+}
diff --git 
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/string/HexStringUtilTest.java
 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/string/HexStringUtilTest.java
new file mode 100644
index 00000000000..99cda499fa0
--- /dev/null
+++ 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/string/HexStringUtilTest.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.util.string;
+
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class HexStringUtilTest {
+    
+    @Test
+    void assertToHexString() {
+        assertThat(HexStringUtil.toHexString(new byte[]{(byte) 1}), is("01"));
+        assertThat(HexStringUtil.toHexString(new byte[]{(byte) 100}), 
is("64"));
+        assertThat(HexStringUtil.toHexString(new byte[]{(byte) 255, (byte) 
256}), is("ff00"));
+    }
+}

Reply via email to