Consolidate HexTool methods and add more tests

This commit is contained in:
P0nk
2022-02-14 19:05:50 +01:00
parent 1daddbf302
commit 084e7b22fa
8 changed files with 61 additions and 41 deletions

View File

@@ -1335,7 +1335,7 @@ public class Client extends ChannelInboundHandlerAdapter {
try {
MessageDigest digester = MessageDigest.getInstance(type);
digester.update(password.getBytes(StandardCharsets.UTF_8), 0, password.length());
return HexTool.toString(digester.digest()).replace(" ", "").toLowerCase().equals(hash);
return HexTool.toHexString(digester.digest()).replace(" ", "").toLowerCase().equals(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Encoding the string failed", e);
}

View File

@@ -180,7 +180,7 @@ public class MapleAESOFB {
@Override
public String toString() {
return "IV: " + HexTool.toString(this.iv);
return "IV: " + HexTool.toHexString(this.iv);
}
private static byte[] funnyShit(byte inputByte, byte[] in) {

View File

@@ -35,9 +35,9 @@ public class InPacketLogger extends ChannelInboundHandlerAdapter implements Pack
final String opcodeName = getRecvOpcodeName(opcode);
final String prefix = opcodeName == null ? "<UnknownPacket> " : "";
log.debug("{}ClientSend:{} [{}] ({}) <HEX> {} <TEXT> {}", prefix, opcodeName, opcodeHex, packetLength,
HexTool.toString(content), HexTool.toStringFromAscii(content));
HexTool.toHexString(content), HexTool.toStringFromAscii(content));
} else {
log.debug(HexTool.toString(new byte[]{content[0], content[1]}) + "...");
log.debug(HexTool.toHexString(new byte[]{content[0], content[1]}) + "...");
}
}

View File

@@ -75,7 +75,7 @@ public class MonitoredChrLogger {
return;
}
String packet = packetContent.length > 0 ? HexTool.toString(packetContent) : "<empty>";
String packet = packetContent.length > 0 ? HexTool.toHexString(packetContent) : "<empty>";
log.info("{}-{} {}-{}", c.getAccountName(), chr.getName(), packetId, packet);
}

View File

@@ -36,9 +36,9 @@ public class OutPacketLogger extends ChannelOutboundHandlerAdapter implements Pa
String opcodeName = getSendOpcodeName(opcode);
String prefix = opcodeName == null ? "<UnknownPacket> " : "";
log.debug("{}ServerSend:{} [{}] ({}) <HEX> {} <TEXT> {}", prefix, opcodeName, opcodeHex, packetLength,
HexTool.toString(content), HexTool.toStringFromAscii(content));
HexTool.toHexString(content), HexTool.toStringFromAscii(content));
} else {
log.debug(HexTool.toString(new byte[]{content[0], content[1]}) + " ...");
log.debug(HexTool.toHexString(new byte[]{content[0], content[1]}) + " ...");
}
}

View File

@@ -50,7 +50,7 @@ public final class LoginPasswordHandler implements PacketHandler {
private static String hashpwSHA512(String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest digester = MessageDigest.getInstance("SHA-512");
digester.update(pwd.getBytes(StandardCharsets.UTF_8), 0, pwd.length());
return HexTool.toString(digester.digest()).replace(" ", "").toLowerCase();
return HexTool.toHexString(digester.digest()).replace(" ", "").toLowerCase();
}
@Override
@@ -67,7 +67,7 @@ public final class LoginPasswordHandler implements PacketHandler {
p.skip(6); // localhost masked the initial part with zeroes...
byte[] hwidNibbles = p.readBytes(4);
Hwid hwid = new Hwid(HexTool.bytesToHex(hwidNibbles));
Hwid hwid = new Hwid(HexTool.toCompactHexString(hwidNibbles));
int loginok = c.login(login, pwd, hwid);

View File

@@ -22,30 +22,44 @@
package tools;
import constants.string.CharsetConstants;
import io.netty.buffer.ByteBufUtil;
import java.util.HexFormat;
// TODO: use HexFormat from Java 17
/**
* Handles converting back and forth from byte arrays to hex strings.
*/
public class HexTool {
/**
* Convert a byte array to its hex string representation.
* Each byte value is equivalent to two hex characters delimited by a space.
* Convert a byte array to its hex string representation (upper case).
* Each byte value is converted to two hex characters delimited by a space.
*
* @param bytes Byte array to convert to a hex string.
* Example: {1, 16, 127, -1} is converted to "01 F0 7F FF"
* @return The hex string
*/
public static String toString(byte[] bytes) {
public static String toHexString(byte[] bytes) {
return HexFormat.ofDelimiter(" ").withUpperCase().formatHex(bytes);
}
/**
* Convert a hex string to its byte array representation. Two consecutive hex characters are equivalent to one byte.
* Convert a byte array to its hex string representation (upper case).
* Like {@link #toHexString(byte[]) HexTool.toString}, but with no space delimiter.
*
* @param hexString Hex string to convert to bytes. Hex character pairs may be delimited by a space or not (compact)
* @return The compact hex string
*/
public static String toCompactHexString(byte[] bytes) {
return HexFormat.of().withUpperCase().formatHex(bytes);
}
/**
* Convert a hex string to its byte array representation. Two consecutive hex characters are converted to one byte.
*
* @param hexString Hex string to convert to bytes. May be lower or upper case, and hex character pairs may be
* delimited by a space or not.
* Example: "01 10 7F FF" is converted to {1, 16, 127, -1}.
* The following hex strings are considered identical and are converted to the same byte array:
* "01 10 7F FF", "01107FFF", "01 10 7f ff", "01107fff"
* @return The byte array
*/
public static byte[] toBytes(String hexString) {
@@ -57,27 +71,19 @@ public class HexTool {
}
public static String toStringFromAscii(final byte[] bytes) {
byte[] ret = new byte[bytes.length];
for (int x = 0; x < bytes.length; x++) {
if (bytes[x] < 32 && bytes[x] >= 0) {
ret[x] = '.';
byte[] filteredBytes = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
if (isSpecialCharacter(bytes[i])) {
filteredBytes[i] = '.';
} else {
int chr = ((short) bytes[x]) & 0xFF;
ret[x] = (byte) chr;
filteredBytes[i] = (byte) (bytes[i] & 0xFF);
}
}
return new String(ret, CharsetConstants.CHARSET);
return new String(filteredBytes, CharsetConstants.CHARSET);
}
/**
* Get upper case hex dump
*/
public static String bytesToHex(byte[] bytes) {
return ByteBufUtil.hexDump(bytes).toUpperCase();
}
public static byte[] hexToBytes(String hex) {
return ByteBufUtil.decodeHexDump(hex);
private static boolean isSpecialCharacter(byte asciiCode) {
return asciiCode >= 0 && asciiCode <= 31;
}
}