]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/util/PEM.java
upd: convert to PostgreSQL
[gigi.git] / src / org / cacert / gigi / util / PEM.java
index 0616270533ceac10fd0ec63249704842f2fed16f..049822c1b5eabcb306624e86e816101812bce446 100644 (file)
@@ -1,12 +1,15 @@
 package org.cacert.gigi.util;
 
 import java.util.Base64;
+import java.util.regex.Pattern;
 
 public class PEM {
 
+    public static final Pattern LINE = Pattern.compile("(.{64})(?=.)");
+
     public static String encode(String type, byte[] data) {
         return "-----BEGIN " + type + "-----\n" + //
-                Base64.getEncoder().encodeToString(data).replaceAll("(.{64})(?=.)", "$1\n") + //
+                formatBase64(data) + //
                 "\n-----END " + type + "-----";
     }
 
@@ -18,4 +21,8 @@ public class PEM {
         return Base64.getDecoder().decode(data);
 
     }
+
+    public static String formatBase64(byte[] bytes) {
+        return LINE.matcher(Base64.getEncoder().encodeToString(bytes)).replaceAll("$1\n");
+    }
 }