Skip to content

Commit 3cd9c6e

Browse files
authored
Merge pull request #2034 from IABTechLab/sch-UID2-5854_key_id_patch
writing key id as 3 bytes big endian format
2 parents fa29edd + 14c260a commit 3cd9c6e

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/main/java/com/uid2/operator/service/V4TokenUtils.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import javax.crypto.spec.IvParameterSpec;
55
import javax.crypto.spec.SecretKeySpec;
66
import io.vertx.core.buffer.Buffer;
7-
87
import java.io.ByteArrayOutputStream;
98
import java.util.Arrays;
109

@@ -14,18 +13,22 @@ public final class V4TokenUtils {
1413
private V4TokenUtils() {
1514
}
1615

16+
private static byte[] getKeyIdBytes(int keyId) {
17+
return new byte[] {
18+
(byte) ((keyId >> 16) & 0xFF), // MSB
19+
(byte) ((keyId >> 8) & 0xFF), // Middle
20+
(byte) (keyId & 0xFF), // LSB
21+
};
22+
}
23+
1724
public static byte[] buildAdvertisingIdV4(byte metadata, byte[] firstLevelHash, int keyId, String key, String salt) throws Exception {
1825
byte[] firstLevelHashLast16Bytes = Arrays.copyOfRange(firstLevelHash, firstLevelHash.length - 16, firstLevelHash.length);
1926
byte[] iv = V4TokenUtils.generateIV(salt, firstLevelHashLast16Bytes, metadata, keyId);
2027
byte[] encryptedFirstLevelHash = V4TokenUtils.encryptHash(key, firstLevelHashLast16Bytes, iv);
2128

2229
Buffer buffer = Buffer.buffer();
2330
buffer.appendByte(metadata);
24-
buffer.appendBytes(new byte[] {
25-
(byte) (keyId & 0xFF), // LSB
26-
(byte) ((keyId >> 8) & 0xFF), // Middle
27-
(byte) ((keyId >> 16) & 0xFF) // MSB
28-
});
31+
buffer.appendBytes(getKeyIdBytes(keyId));
2932
buffer.appendBytes(iv);
3033
buffer.appendBytes(encryptedFirstLevelHash);
3134

@@ -40,7 +43,7 @@ public static byte[] generateIV(String salt, byte[] firstLevelHashLast16Bytes, b
4043
ivBase.write(salt.getBytes());
4144
ivBase.write(firstLevelHashLast16Bytes);
4245
ivBase.write(metadata);
43-
ivBase.write(keyId);
46+
ivBase.write(getKeyIdBytes(keyId));
4447
return Arrays.copyOfRange(EncodingUtils.getSha256Bytes(ivBase.toByteArray()), 0, IV_LENGTH);
4548
}
4649

src/test/java/com/uid2/operator/service/V4TokenUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void testBuildAdvertisingIdV4() throws Exception {
2828

2929
byte extractedMetadata = v4UID[0];
3030
byte[] keyIdBytes = Arrays.copyOfRange(v4UID, 1, 4);
31-
int extractedKeyId = (keyIdBytes[0] & 0xFF) | ((keyIdBytes[1] & 0xFF) << 8) | ((keyIdBytes[2] & 0xFF) << 16);
31+
int extractedKeyId = ((keyIdBytes[0] & 0xFF) << 16) | ((keyIdBytes[1] & 0xFF) << 8) | (keyIdBytes[2] & 0xFF);
3232
byte[] extractedIV = Arrays.copyOfRange(v4UID, 4, 16);
3333
byte[] extractedEncryptedHash = Arrays.copyOfRange(v4UID, 16, 32);
3434
byte extractedChecksum = v4UID[32];

0 commit comments

Comments
 (0)