use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testThreadSafety_manipulateKeyset_shouldWork.
@Test
public void testThreadSafety_manipulateKeyset_shouldWork() throws Exception {
final KeysetManager manager = KeysetManager.withEmptyKeyset();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
manipulateKeyset(manager);
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
manipulateKeyset(manager);
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
manipulateKeyset(manager);
}
});
thread1.start();
thread2.start();
thread3.start();
// Wait until all threads finished.
thread1.join();
thread2.join();
thread3.join();
Keyset keyset = manager.getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(12);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testEnable_shouldEnableKey.
@Test
public void testEnable_shouldEnableKey() throws Exception {
int keyId = 42;
KeysetHandle handle = KeysetHandle.fromKeyset(TestUtil.createKeyset(createDisabledKey(keyId)));
Keyset keyset = KeysetManager.withKeysetHandle(handle).enable(keyId).getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(1);
assertThat(keyset.getKey(0).getKeyId()).isEqualTo(keyId);
assertThat(keyset.getKey(0).getStatus()).isEqualTo(KeyStatusType.ENABLED);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testRotate_existingKeyset_shouldAddNewKeyAndSetPrimaryKeyId.
@Test
public void testRotate_existingKeyset_shouldAddNewKeyAndSetPrimaryKeyId() throws Exception {
// Need to test the deprecated function
@SuppressWarnings("deprecation") KeysetHandle existing = KeysetManager.withEmptyKeyset().rotate(KeyTemplates.get("HMAC_SHA256_128BITTAG").getProto()).getKeysetHandle();
// Need to test the deprecated function
@SuppressWarnings("deprecation") Keyset keyset = KeysetManager.withKeysetHandle(existing).rotate(KeyTemplates.get("HMAC_SHA256_256BITTAG").getProto()).getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(2);
assertThat(keyset.getPrimaryKeyId()).isEqualTo(keyset.getKey(1).getKeyId());
TestUtil.assertHmacKey(KeyTemplates.get("HMAC_SHA256_128BITTAG"), keyset.getKey(0));
TestUtil.assertHmacKey(KeyTemplates.get("HMAC_SHA256_256BITTAG"), keyset.getKey(1));
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testAdd_existingKeySet_shouldAddNewKey.
@Test
public void testAdd_existingKeySet_shouldAddNewKey() throws Exception {
KeyTemplate kt1 = AesGcmKeyManager.aes128GcmTemplate();
KeysetHandle existing = KeysetManager.withEmptyKeyset().add(kt1).getKeysetHandle();
KeyTemplate kt2 = AesGcmKeyManager.aes256GcmTemplate();
Keyset keyset = KeysetManager.withKeysetHandle(existing).add(kt2).getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(2);
// None of the keys are primary.
assertThat(keyset.getPrimaryKeyId()).isEqualTo(0);
Keyset.Key key1 = keyset.getKey(0);
assertThat(key1.getStatus()).isEqualTo(KeyStatusType.ENABLED);
assertThat(key1.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
assertThat(key1.hasKeyData()).isTrue();
assertThat(key1.getKeyData().getTypeUrl()).isEqualTo(kt1.getTypeUrl());
AesGcmKeyFormat aesGcmKeyFormat1 = AesGcmKeyFormat.parseFrom(kt1.getValue(), ExtensionRegistryLite.getEmptyRegistry());
AesGcmKey aesGcmKey1 = AesGcmKey.parseFrom(key1.getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(aesGcmKey1.getKeyValue().size()).isEqualTo(aesGcmKeyFormat1.getKeySize());
Keyset.Key key2 = keyset.getKey(1);
assertThat(key2.getStatus()).isEqualTo(KeyStatusType.ENABLED);
assertThat(key2.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
assertThat(key2.hasKeyData()).isTrue();
assertThat(key2.getKeyData().getTypeUrl()).isEqualTo(kt2.getTypeUrl());
AesGcmKeyFormat aesGcmKeyFormat2 = AesGcmKeyFormat.parseFrom(kt2.getValue(), ExtensionRegistryLite.getEmptyRegistry());
AesGcmKey aesGcmKey2 = AesGcmKey.parseFrom(key2.getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(aesGcmKey2.getKeyValue().size()).isEqualTo(aesGcmKeyFormat2.getKeySize());
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testAddNewKey_onePrimary.
@Test
public void testAddNewKey_onePrimary() throws Exception {
KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
int keyId = keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, true);
Keyset keyset = keysetManager.getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(1);
assertThat(keyset.getPrimaryKeyId()).isEqualTo(keyId);
TestUtil.assertHmacKey(KeyTemplates.get("HMAC_SHA256_128BITTAG"), keyset.getKey(0));
}
Aggregations