]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/util/KeyUtils.java
3f9ec08fd55d92f91d7a99a4dc37605d917cf8c1
[signal-cli] / src / main / java / org / asamk / signal / manager / util / KeyUtils.java
1 package org.asamk.signal.manager.util;
2
3 import org.asamk.signal.util.RandomUtils;
4 import org.signal.zkgroup.InvalidInputException;
5 import org.signal.zkgroup.profiles.ProfileKey;
6 import org.whispersystems.signalservice.api.kbs.MasterKey;
7 import org.whispersystems.util.Base64;
8
9 public class KeyUtils {
10
11 private KeyUtils() {
12 }
13
14 public static String createSignalingKey() {
15 return getSecret(52);
16 }
17
18 public static ProfileKey createProfileKey() {
19 try {
20 return new ProfileKey(getSecretBytes(32));
21 } catch (InvalidInputException e) {
22 throw new AssertionError("Profile key is guaranteed to be 32 bytes here");
23 }
24 }
25
26 public static String createPassword() {
27 return getSecret(18);
28 }
29
30 public static byte[] createStickerUploadKey() {
31 return getSecretBytes(32);
32 }
33
34 public static MasterKey createMasterKey() {
35 return MasterKey.createNew(RandomUtils.getSecureRandom());
36 }
37
38 private static String getSecret(int size) {
39 byte[] secret = getSecretBytes(size);
40 return Base64.encodeBytes(secret);
41 }
42
43 public static byte[] getSecretBytes(int size) {
44 byte[] secret = new byte[size];
45 RandomUtils.getSecureRandom().nextBytes(secret);
46 return secret;
47 }
48 }