package org.asamk.signal.util;
-import java.io.File;
-import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
+import com.fasterxml.jackson.databind.JsonNode;
+
+import java.io.InvalidObjectException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.HashMap;
+import java.util.Map;
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 Map<String, String> getQueryMap(String query) {
+ String[] params = query.split("&");
+ Map<String, String> map = new HashMap<>();
+ for (String param : params) {
+ String name = null;
+ final String[] paramParts = param.split("=");
+ try {
+ name = URLDecoder.decode(paramParts[0], "utf-8");
+ } catch (UnsupportedEncodingException e) {
+ // Impossible
+ }
+ String value = null;
+ try {
+ value = URLDecoder.decode(paramParts[1], "utf-8");
+ } catch (UnsupportedEncodingException e) {
+ // Impossible
+ }
+ map.put(name, value);
}
+ return map;
}
- public static File createTempFile() throws IOException {
- return File.createTempFile("signal_tmp_", ".tmp");
+ 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;
}
}