use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class UtilTest method testGetKeysetInfo.
/**
* Tests that getKeysetInfo doesn't contain key material.
*/
@Test
public void testGetKeysetInfo() throws Exception {
String keyValue = "01234567890123456";
Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
assertTrue(keyset.toString().contains(keyValue));
KeysetInfo keysetInfo = Util.getKeysetInfo(keyset);
assertFalse(keysetInfo.toString().contains(keyValue));
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class UtilTest method testValidateKeyset_noEnabledKey_shouldFail.
@Test
public void testValidateKeyset_noEnabledKey_shouldFail() throws Exception {
String keyValue = "01234567890123456";
// No ENABLED key.
Keyset invalidKeyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DISABLED, OutputPrefixType.TINK), TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DESTROYED, OutputPrefixType.TINK));
GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
assertExceptionContains(e, "keyset must contain at least one ENABLED key");
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class UtilTest method testValidateKeyset_primaryKeyIsDisabled_shouldFail.
@Test
public void testValidateKeyset_primaryKeyIsDisabled_shouldFail() throws Exception {
String keyValue = "01234567890123456";
// Primary key is disabled.
Keyset invalidKeyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DISABLED, OutputPrefixType.TINK), TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 43, KeyStatusType.ENABLED, OutputPrefixType.TINK));
GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
assertExceptionContains(e, "keyset doesn't contain a valid primary key");
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class UtilTest method testValidateKeyset_noPrimaryKey_shouldFail.
@Test
public void testValidateKeyset_noPrimaryKey_shouldFail() throws Exception {
String keyValue = "01234567890123456";
// No primary key.
Keyset invalidKeyset = Keyset.newBuilder().addKey(Keyset.Key.newBuilder().setKeyData(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16)).setKeyId(1).setStatus(KeyStatusType.ENABLED).setOutputPrefixType(OutputPrefixType.TINK).build()).build();
GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
assertExceptionContains(e, "keyset doesn't contain a valid primary key");
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testAddNewKey_addThenDestroy.
@Test
public void testAddNewKey_addThenDestroy() throws Exception {
KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, true);
int secondaryKeyId = keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, false);
keysetManager.destroy(secondaryKeyId);
Keyset keyset = keysetManager.getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(2);
// One of the two keys is destroyed and doesn't have keyData anymore.
assertTrue(!keyset.getKey(0).hasKeyData() || !keyset.getKey(1).hasKeyData());
}
Aggregations