Skip to content

Commit

Permalink
Changes on keyId generation
Browse files Browse the repository at this point in the history
This changes avoid some future problems with message ID's
  • Loading branch information
Darker935 authored Jun 16, 2024
1 parent 0873de8 commit 096839e
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.auties.protobuf.annotation.ProtobufProperty;
import it.auties.protobuf.model.ProtobufMessage;
import it.auties.protobuf.model.ProtobufType;
import it.auties.whatsapp.api.ClientType;
import it.auties.whatsapp.model.info.ChatMessageInfo;
import it.auties.whatsapp.model.jid.Jid;
import it.auties.whatsapp.util.Bytes;
Expand Down Expand Up @@ -59,6 +60,75 @@ public static String randomId() {
.toUpperCase(Locale.ROOT);
}

/**
* Generates a random message id based on the Client Type. Generation methods are taken from WEB and Android code
* @param jid senderJid
* @param clientType clientType (Mobile or Web)
* @return a non-null String
*/

public static String randomIdV2(Jid jid, ClientType... clientType) {
var type = Objects.requireNonNullElse(clientType[0], ClientType.WEB);
return switch (type) {
case ClientType.WEB -> randomWebKeyId(jid);
case ClientType.MOBILE -> randomMobileKeyId(jid);
};
}

private static String randomWebKeyId(Jid jid) {
try {
var random = new Random();
var meUser = "%s@%s".formatted(jid.user(), "@c.us");
long timeSeconds = Instant.now().getEpochSecond();
byte[] randomBytes = new byte[16];
random.nextBytes(randomBytes);
var buffer = ByteBuffer.allocate(Long.BYTES + meUser.length() + randomBytes.length);
buffer.putLong(timeSeconds);
buffer.put(meUser.getBytes());
buffer.put(randomBytes);
var digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(buffer.array());
byte[] truncatedHash = new byte[9];
System.arraycopy(hash, 0, truncatedHash, 0, 9);
return "3EB0" + HexFormat.of().formatHex(truncatedHash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

private static String randomMobileKeyId(Jid jid) {
try {
var random = new Random();
var messageDigest = MessageDigest.getInstance("MD5");
var meUser = jid.toSimpleJid().toString().getBytes();
long timeMillis = System.currentTimeMillis();
byte[] bArr = new byte[8];
for (int i = 7; i >= 0; i--) {
bArr[i] = (byte) timeMillis;
timeMillis >>= 8;
}
messageDigest.update(bArr);
messageDigest.update(meUser);
byte[] bArr2 = new byte[16];
random.nextBytes(bArr2);
messageDigest.update(bArr2);
var digested = messageDigest.digest();
char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] cArr2 = new char[digested.length * 2];
int i = 0;
for (byte b : digested) {
int i2 = b & 255;
int i3 = i + 1;
cArr2[i] = cArr[i2 >>> 4];
i = i3 + 1;
cArr2[i3] = cArr[i2 & 15];
}
return new String(cArr2);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public Jid chatJid() {
return chatJid;
}
Expand Down

0 comments on commit 096839e

Please sign in to comment.