From e51c4c17015b2b6021128e6b0cc9267224633d64 Mon Sep 17 00:00:00 2001 From: Pixirora Date: Thu, 8 Aug 2024 15:44:10 -0400 Subject: [PATCH] Improve toHex function Co-authored-by: LostLuma --- .../kitten_heart/impl/network/Encryption.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/network/Encryption.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/network/Encryption.java index 027e9ad..3edc017 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/network/Encryption.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/network/Encryption.java @@ -25,22 +25,16 @@ public static byte[] getMd5Digest(String input) { } public static String bytesToHex(byte[] bytes) { - List hexDigits = new ArrayList<>(); + StringBuilder hexString = new StringBuilder(bytes.length * 2); for (byte rawByte : bytes) { int processedByte = Byte.toUnsignedInt(rawByte); // Example byte: 11 01 10 00 - hexDigits.add(processedByte >> 4); // Grabs the part that is 11 01 - hexDigits.add(processedByte & 15); // Grabs the part that is 10 00 (15 is 2 << 4 - 1) + hexString.append(Integer.toHexString(processedByte >> 0b100)); // Grabs the part that is 11 01 + hexString.append(Integer.toHexString(processedByte & 0b1111)); // Grabs the part that is 10 00 } - String output = ""; - - for (int i : hexDigits) { - output += Integer.toHexString(i); - } - - return output; + return hexString.toString(); } }