Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HexStringUtil #34082

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Loading