[Fix] Fixed StringHelper.fromPrettyHexString()

This commit is contained in:
Robert von Burg 2017-08-05 11:50:49 +02:00
parent 40c9d92bb2
commit f82d593261
1 changed files with 10 additions and 16 deletions

View File

@ -15,7 +15,6 @@
*/ */
package li.strolch.utils.helper; package li.strolch.utils.helper;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -61,10 +60,6 @@ public class StringHelper {
return String.format("%02x", data); return String.format("%02x", data);
} }
public static byte fromHexByte(String encoded) {
return Byte.valueOf(encoded, 16);
}
public static String toPrettyHexString(byte[] raw) { public static String toPrettyHexString(byte[] raw) {
try { try {
byte[] hex = new byte[3 * raw.length + (raw.length / 8)]; byte[] hex = new byte[3 * raw.length + (raw.length / 8)];
@ -91,14 +86,13 @@ public class StringHelper {
} }
public static byte[] fromPrettyHexString(String prettyHex) { public static byte[] fromPrettyHexString(String prettyHex) {
String s = prettyHex.replace(" ", "");
ByteArrayOutputStream out = new ByteArrayOutputStream(); int len = s.length();
String[] split = prettyHex.split(" "); byte[] data = new byte[len / 2];
for (String encoded : split) { for (int i = 0; i < len; i += 2) {
out.write(fromHexByte(encoded)); data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
} }
return data;
return out.toByteArray();
} }
/** /**
@ -236,8 +230,8 @@ public class StringHelper {
} }
/** /**
* Generates the SHA1 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array * Generates the SHA1 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array to
* to a Hex String which is printable * a Hex String which is printable
* *
* @param bytes * @param bytes
* the bytes to hash * the bytes to hash
@ -274,8 +268,8 @@ public class StringHelper {
} }
/** /**
* Generates the SHA1 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array * Generates the SHA1 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array to
* to a Hex String which is printable * a Hex String which is printable
* *
* @param bytes * @param bytes
* the bytes to hash * the bytes to hash