]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/util/Util.java
Extract static methods from Main
[signal-cli] / src / main / java / org / asamk / signal / util / Util.java
index 19695ec6c9e9508a0249807fe4b72c14ff236ec1..e7a6866842ed50156373e96bcfe29c16f428983b 100644 (file)
@@ -1,33 +1,53 @@
 package org.asamk.signal.util;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import org.asamk.signal.GroupIdFormatException;
 import org.whispersystems.signalservice.internal.util.Base64;
 
-import java.io.File;
 import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
+import java.io.InvalidObjectException;
 
 public class Util {
-    public static String getSecret(int size) {
-        byte[] secret = getSecretBytes(size);
-        return Base64.encodeBytes(secret);
+
+    private Util() {
     }
 
-    public static byte[] getSecretBytes(int size) {
-        byte[] secret = new byte[size];
-        getSecureRandom().nextBytes(secret);
-        return secret;
+    public static String formatSafetyNumber(String digits) {
+        final int partCount = 12;
+        int partSize = digits.length() / partCount;
+        StringBuilder f = new StringBuilder(digits.length() + partCount);
+        for (int i = 0; i < partCount; i++) {
+            f.append(digits, i * partSize, (i * partSize) + partSize).append(" ");
+        }
+        return f.toString();
     }
 
-    private static SecureRandom getSecureRandom() {
-        try {
-            return SecureRandom.getInstance("SHA1PRNG");
-        } catch (NoSuchAlgorithmException e) {
-            throw new AssertionError(e);
+    public static String join(CharSequence separator, Iterable<? extends CharSequence> list) {
+        StringBuilder buf = new StringBuilder();
+        for (CharSequence str : list) {
+            if (buf.length() > 0) {
+                buf.append(separator);
+            }
+            buf.append(str);
+        }
+
+        return buf.toString();
+    }
+
+    public static JsonNode getNotNullNode(JsonNode parent, String name) throws InvalidObjectException {
+        JsonNode node = parent.get(name);
+        if (node == null) {
+            throw new InvalidObjectException(String.format("Incorrect file format: expected parameter %s not found ", name));
         }
+
+        return node;
     }
 
-    public static File createTempFile() throws IOException {
-        return File.createTempFile("signal_tmp_", ".tmp");
+    public static byte[] decodeGroupId(String groupId) throws GroupIdFormatException {
+        try {
+            return Base64.decode(groupId);
+        } catch (IOException e) {
+            throw new GroupIdFormatException(groupId, e);
+        }
     }
 }