Skip to content

Commit a63e1fb

Browse files
committed
Updated var naming and Cipher to ThreadLocal
1 parent 254a183 commit a63e1fb

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55
import javax.crypto.spec.SecretKeySpec;
66
import io.vertx.core.buffer.Buffer;
77
import java.io.ByteArrayOutputStream;
8+
import java.security.GeneralSecurityException;
89
import java.util.Arrays;
910

1011
public final class V4TokenUtils {
12+
private static final ThreadLocal<Cipher> CIPHER = ThreadLocal.withInitial(() -> {
13+
try {
14+
return Cipher.getInstance("AES/CTR/NoPadding");
15+
} catch (GeneralSecurityException e) {
16+
throw new RuntimeException(e);
17+
}
18+
});
1119
private static final int IV_LENGTH = 12;
1220

1321
private V4TokenUtils() {
@@ -49,7 +57,7 @@ public static byte[] generateIV(String salt, byte[] firstLevelHashLast16Bytes, b
4957

5058
public static byte[] encryptHash(String encryptionKey, byte[] hash, byte[] iv) throws Exception {
5159
// Set up AES256-CTR cipher
52-
Cipher aesCtr = Cipher.getInstance("AES/CTR/NoPadding");
60+
Cipher aesCtr = CIPHER.get();
5361
SecretKeySpec secretKey = new SecretKeySpec(encryptionKey.getBytes(), "AES");
5462
IvParameterSpec ivSpec = new IvParameterSpec(padIV16Bytes(iv));
5563

src/test/java/com/uid2/operator/UIDOperatorVerticleTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,11 @@ private void assertTokenStatusMetrics(Integer siteId, TokenResponseStatsCollecto
680680
assertEquals(1, actual);
681681
}
682682

683-
private byte[] getAdvertisingIdFromIdentity(IdentityType identityType, String identityString, String firstLevelSalt, SaltEntry salt, boolean useV4Uid, boolean usePrevUid) throws Exception {
684-
if (useV4Uid) {
685-
return getAdvertisingIdFromIdentity(identityType, identityString, firstLevelSalt, usePrevUid ? salt.previousKeySalt() : salt.currentKeySalt());
683+
private byte[] getAdvertisingIdFromIdentity(IdentityType identityType, String identityString, String firstLevelSalt, SaltEntry salt, boolean getV4Uid, boolean getPrevUid) throws Exception {
684+
if (getV4Uid) {
685+
return getAdvertisingIdFromIdentity(identityType, identityString, firstLevelSalt, getPrevUid ? salt.previousKeySalt() : salt.currentKeySalt());
686686
} else {
687-
return getAdvertisingIdFromIdentity(identityType, identityString, firstLevelSalt, usePrevUid ? salt.previousSalt() : salt.currentSalt());
687+
return getAdvertisingIdFromIdentity(identityType, identityString, firstLevelSalt, getPrevUid ? salt.previousSalt() : salt.currentSalt());
688688
}
689689
}
690690

0 commit comments

Comments
 (0)