1 package org
.asamk
.signal
.manager
.util
;
3 import org
.asamk
.signal
.util
.RandomUtils
;
4 import org
.signal
.zkgroup
.InvalidInputException
;
5 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
6 import org
.whispersystems
.libsignal
.IdentityKey
;
7 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
8 import org
.whispersystems
.libsignal
.InvalidKeyException
;
9 import org
.whispersystems
.libsignal
.ecc
.Curve
;
10 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
11 import org
.whispersystems
.libsignal
.ecc
.ECPrivateKey
;
12 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
13 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
14 import org
.whispersystems
.libsignal
.util
.Medium
;
15 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
16 import org
.whispersystems
.util
.Base64
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 public class KeyUtils
{
26 public static IdentityKeyPair
generateIdentityKeyPair() {
27 ECKeyPair djbKeyPair
= Curve
.generateKeyPair();
28 IdentityKey djbIdentityKey
= new IdentityKey(djbKeyPair
.getPublicKey());
29 ECPrivateKey djbPrivateKey
= djbKeyPair
.getPrivateKey();
31 return new IdentityKeyPair(djbIdentityKey
, djbPrivateKey
);
34 public static List
<PreKeyRecord
> generatePreKeyRecords(final int offset
, final int batchSize
) {
35 List
<PreKeyRecord
> records
= new ArrayList
<>(batchSize
);
36 for (int i
= 0; i
< batchSize
; i
++) {
37 int preKeyId
= (offset
+ i
) % Medium
.MAX_VALUE
;
38 ECKeyPair keyPair
= Curve
.generateKeyPair();
39 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
46 public static SignedPreKeyRecord
generateSignedPreKeyRecord(
47 final IdentityKeyPair identityKeyPair
, final int signedPreKeyId
49 ECKeyPair keyPair
= Curve
.generateKeyPair();
52 signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
53 } catch (InvalidKeyException e
) {
54 throw new AssertionError(e
);
56 return new SignedPreKeyRecord(signedPreKeyId
, System
.currentTimeMillis(), keyPair
, signature
);
59 public static String
createSignalingKey() {
63 public static ProfileKey
createProfileKey() {
65 return new ProfileKey(getSecretBytes(32));
66 } catch (InvalidInputException e
) {
67 throw new AssertionError("Profile key is guaranteed to be 32 bytes here");
71 public static String
createPassword() {
75 public static byte[] createStickerUploadKey() {
76 return getSecretBytes(32);
79 public static MasterKey
createMasterKey() {
80 return MasterKey
.createNew(RandomUtils
.getSecureRandom());
83 private static String
getSecret(int size
) {
84 byte[] secret
= getSecretBytes(size
);
85 return Base64
.encodeBytes(secret
);
88 public static byte[] getSecretBytes(int size
) {
89 byte[] secret
= new byte[size
];
90 RandomUtils
.getSecureRandom().nextBytes(secret
);