Skip to content

Commit

Permalink
Add HexStringUtil (#34082)
Browse files Browse the repository at this point in the history
* Add HexStringUtil

* Add HexStringUtil
  • Loading branch information
terrymanu authored Dec 16, 2024
1 parent a1c9837 commit d0074fb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
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"));
}
}

0 comments on commit d0074fb

Please sign in to comment.