Search in sources :

Example 86 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class JsonKeysetReaderTest method testReadKeyset_negativeKeyId_works.

@Test
public void testReadKeyset_negativeKeyId_works() throws Exception {
    String jsonKeysetString = createJsonKeysetWithId("-21");
    Keyset keyset = JsonKeysetReader.withString(jsonKeysetString).read();
    assertThat(keyset.getPrimaryKeyId()).isEqualTo(-21);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Example 87 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testValidateKeyset_multiplePrimaryKeys_shouldFail.

@Test
public void testValidateKeyset_multiplePrimaryKeys_shouldFail() throws Exception {
    String keyValue = "01234567890123456";
    // Multiple primary keys.
    Keyset invalidKeyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK), TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
    assertExceptionContains(e, "keyset contains multiple primary keys");
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 88 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testValidateKeyset_withDestroyedKey_shouldWork.

@Test
public void testValidateKeyset_withDestroyedKey_shouldWork() throws Exception {
    String keyValue = "01234567890123456";
    Keyset validKeyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK), TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DESTROYED, OutputPrefixType.TINK));
    try {
        Util.validateKeyset(validKeyset);
    } catch (GeneralSecurityException e) {
        fail("Valid keyset, should not fail: " + e);
    }
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 89 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testValidateKeyset_shouldWork.

@Test
public void testValidateKeyset_shouldWork() throws Exception {
    String keyValue = "01234567890123456";
    Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), -42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
    try {
        Util.validateKeyset(keyset);
    } catch (GeneralSecurityException e) {
        fail("Valid keyset; should not throw Exception: " + e);
    }
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 90 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetManagerTest method testRotate_shouldAddNewKeyAndSetPrimaryKeyId.

@Test
public void testRotate_shouldAddNewKeyAndSetPrimaryKeyId() throws Exception {
    // Create a keyset that contains a single HmacKey.
    KeyTemplate template = KeyTemplates.get("HMAC_SHA256_128BITTAG");
    // Need to test the deprecated function
    @SuppressWarnings("deprecation") Keyset keyset = KeysetManager.withEmptyKeyset().rotate(template.getProto()).getKeysetHandle().getKeyset();
    assertThat(keyset.getKeyCount()).isEqualTo(1);
    assertThat(keyset.getPrimaryKeyId()).isEqualTo(keyset.getKey(0).getKeyId());
    TestUtil.assertHmacKey(template, keyset.getKey(0));
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Aggregations

Keyset (com.google.crypto.tink.proto.Keyset)108 Test (org.junit.Test)81 GeneralSecurityException (java.security.GeneralSecurityException)22 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)17 KeysetHandle (com.google.crypto.tink.KeysetHandle)17 KeyData (com.google.crypto.tink.proto.KeyData)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)12 EncryptedKeyset (com.google.crypto.tink.proto.EncryptedKeyset)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteString (com.google.protobuf.ByteString)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 Key (com.google.crypto.tink.proto.Keyset.Key)9 JsonObject (com.google.gson.JsonObject)9 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeysetReader (com.google.crypto.tink.KeysetReader)7 IOException (java.io.IOException)7 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 Enums (com.google.crypto.tink.subtle.Enums)6 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)6