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 91c75b05a950e..78aa7961c86c6 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.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 @@ private static byte charToByte(final char c) { 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 0000000000000..bdd7aed7e93f1 --- /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 0000000000000..99cda499fa0ee --- /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")); + } +}