-package org.asamk.signal;
+package org.asamk.signal.util;
-import java.io.File;
-import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
+import org.asamk.signal.manager.groups.GroupId;
+import org.asamk.signal.manager.groups.GroupIdFormatException;
+import org.whispersystems.libsignal.util.guava.Optional;
+import org.whispersystems.signalservice.api.push.SignalServiceAddress;
-class Util {
- public static String getSecret(int size) {
- byte[] secret = getSecretBytes(size);
- return Base64.encodeBytes(secret);
+public class Util {
+
+ private Util() {
}
- public static byte[] getSecretBytes(int size) {
- byte[] secret = new byte[size];
- getSecureRandom().nextBytes(secret);
- return secret;
+ public static String getStringIfNotBlank(Optional<String> value) {
+ var string = value.orNull();
+ if (string == null || string.isBlank()) {
+ return null;
+ }
+ return string;
}
- private static SecureRandom getSecureRandom() {
- try {
- return SecureRandom.getInstance("SHA1PRNG");
- } catch (NoSuchAlgorithmException e) {
- throw new AssertionError(e);
+ public static String formatSafetyNumber(String digits) {
+ final var partCount = 12;
+ var partSize = digits.length() / partCount;
+ var f = new StringBuilder(digits.length() + partCount);
+ for (var i = 0; i < partCount; i++) {
+ f.append(digits, i * partSize, (i * partSize) + partSize).append(" ");
}
+ return f.toString();
+ }
+
+ public static GroupId decodeGroupId(String groupId) throws GroupIdFormatException {
+ return GroupId.fromBase64(groupId);
}
- public static File createTempFile() throws IOException {
- return File.createTempFile("signal_tmp_", ".tmp");
+ public static String getLegacyIdentifier(final SignalServiceAddress address) {
+ return address.getNumber().or(() -> address.getUuid().get().toString());
}
}