aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/ui/Utils.java')
-rw-r--r--src/main/ui/Utils.java44
1 files changed, 21 insertions, 23 deletions
diff --git a/src/main/ui/Utils.java b/src/main/ui/Utils.java
index ccb244e..3ed1300 100644
--- a/src/main/ui/Utils.java
+++ b/src/main/ui/Utils.java
@@ -49,19 +49,8 @@ public final class Utils {
}
/**
- * EFFECTS: Pack the big-endian bytes into a 64bit integer.
- * Throws {@link model.asn1.exceptions.ParseException} if the value is too large.
- */
- public static long bytesToLong(Byte[] array) throws ParseException {
- try {
- return new BigInteger(byteToByte(array)).longValueExact();
- } catch (ArithmeticException ignored) {
- throw new ParseException("Value is too large.");
- }
- }
-
- /**
- * EFFECTS: Unpack the multibyte 64bit integer to its shortest array of byte format.
+ * EFFECTS: Unpack the multibyte 64bit integer to its shortest array of byte format. Remove leading empty byte
+ * if that number is not zero.
*/
public static Byte[] valToByte(long val) {
byte[] v = BigInteger.valueOf(val).toByteArray();
@@ -77,15 +66,8 @@ public final class Utils {
}
/**
- * EFFECTS: Parse the two-digit octet string into an unsigned byte, preserving leading zero and negative values.
- * REQUIRES: The input octet must be a two-char string, with each char matching [0-9][A-F].
- */
- public static Byte parseByte(String octet) {
- return (byte) Integer.parseInt(octet, 16);
- }
-
- /**
- * EFFECTS: Decode the input PEM file, with optional check on tags.
+ * EFFECTS: Decode the input PEM file, with optional check on tags. \n must present after each line, optional after
+ * the last.
* Throws {@link ParseException} if the desiredTag is specified but the input does not have the specific tag, or
* if the input does not have any tags at all (not a PEM).
*/
@@ -93,7 +75,7 @@ public final class Utils {
final String str = new String(byteToByte(input), StandardCharsets.UTF_8);
Pattern pattern =
Pattern.compile("^-----BEGIN " + desiredTag
- + "-----$\n^(.*)$\n^-----END " + desiredTag + "-----$",
+ + "-----$\n^(.*)$\n^-----END " + desiredTag + "-----$\n*",
Pattern.DOTALL | Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(str);
if (!matcher.matches()) {
@@ -102,4 +84,20 @@ public final class Utils {
final String b64 = matcher.group(1).replace("\n", "");
return byteToByte(Base64.getDecoder().decode(b64));
}
+
+ /**
+ * EFFECTS: Base64 encode the input bytes and convert them into PEM format.
+ * REQUIRES: desiredTag must be upper case and not empty.
+ */
+ public static String toPEM(Byte[] input, String tag) {
+ StringBuilder builder = new StringBuilder();
+ builder.append("-----BEGIN ");
+ builder.append(tag);
+ builder.append("-----\n");
+ builder.append(Base64.getEncoder().encodeToString(byteToByte(input)));
+ builder.append("\n-----END ");
+ builder.append(tag);
+ builder.append("-----");
+ return builder.toString();
+ }
}