trim whitespace chars before testing if byte array contains only hex or base64 chars

This commit is contained in:
Craig Raw 2025-02-08 09:29:33 +02:00
parent ad60a37d0e
commit f7d5b4fb8f

View file

@ -57,7 +57,7 @@ public class Utils {
return false;
}
for(byte b : bytes) {
for(byte b : trim(bytes)) {
if(!((b >= '0' && b <= '9') || (b >= 'A' && b <= 'F') || (b >= 'a' && b <= 'f'))) {
return false;
}
@ -71,7 +71,7 @@ public class Utils {
return false;
}
for(byte b : bytes) {
for(byte b : trim(bytes)) {
if(!((b >= '0' && b <= '9') || (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b == '+') || (b == '/') || (b == '='))) {
return false;
}
@ -80,6 +80,19 @@ public class Utils {
return true;
}
private static byte[] trim(byte[] bytes) {
int len = bytes.length;
int st = 0;
while ((st < len) && Character.isWhitespace((bytes[st] & 0xff))) {
st++;
}
while ((st < len) && Character.isWhitespace((bytes[len - 1] & 0xff))) {
len--;
}
return ((st > 0) || (len < bytes.length)) ? Arrays.copyOfRange(bytes, st, len) : bytes;
}
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {