From 09f0f9449a10b713207126348105fafec4781bed Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Mon, 24 Jan 2022 16:51:04 +0100 Subject: signature validation changed to work with teh dynamic header version 2. --- .../java/org/uic/barcode/asn1/uper/AsnUtils.java | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java (limited to 'src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java') diff --git a/src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java b/src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java new file mode 100644 index 0000000..dbb95c9 --- /dev/null +++ b/src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java @@ -0,0 +1,59 @@ +package org.uic.barcode.asn1.uper; + +public class AsnUtils { + + + private static byte[] mask = new byte[] { + (byte) 0b1000_0000, + 0b0100_0000, + 0b0010_0000, + 0b0001_0000, + 0b0000_1000, + 0b0000_0100, + 0b0000_0010, + 0b0000_0001, + }; + + + public static byte[] fromBooleanString(final String s) { + + char[] ascii = s.toCharArray(); + if (ascii == null || ascii.length == 0) { + return null; + } + // get length/8 times bytes with 3 bit shifts to the right of the length + final byte[] l_raw = new byte[ascii.length >> 3]; + /* + * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the + * loop. + */ + for (int ii = 0, jj = 0; ii < l_raw.length; ii++, jj += 8) { + for (int bits = 0; bits < mask.length; ++bits) { + if (ascii[jj + bits] == '1') { + l_raw[ii] |= mask[bits]; + } + } + } + return l_raw; + } + + public static String toBooleanString(byte[] bytes) { + StringBuilder sb = new StringBuilder(bytes.length); + for (int i = 0; i < bytes.length*8;i++) { + sb.append(AsnUtils.get(bytes,i) ? "1" : "0"); + } + return sb.toString(); + } + + public static boolean get(byte[] bytes, int index) { + + if (index < 0) { + throw new IndexOutOfBoundsException("Index " + index + " is less than 0"); + } else if (index >= bytes.length * 8) { + throw new IndexOutOfBoundsException("Index " + index + " violates the limit " + bytes.length*8); + } + boolean result = (bytes[index / 8] & mask[index % 8]) != 0; + return result; + } + +} -- cgit v1.2.3