Search in sources :

Example 1 with ChaCha20Poly1305Key

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

the class ChaCha20Poly1305KeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    KeyTemplate keyTemplate = AeadKeyTemplates.CHACHA20_POLY1305;
    ChaCha20Poly1305KeyManager keyManager = new ChaCha20Poly1305KeyManager();
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 10;
    for (int i = 0; i < numTests; i++) {
        ChaCha20Poly1305Key key = (ChaCha20Poly1305Key) keyManager.newKey(keyTemplate.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(32, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = ChaCha20Poly1305Key.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(32, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests * 2, keys.size());
}
Also used : ChaCha20Poly1305Key(com.google.crypto.tink.proto.ChaCha20Poly1305Key) TreeSet(java.util.TreeSet) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 2 with ChaCha20Poly1305Key

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

the class ChaCha20Poly1305KeyManager method getPrimitive.

@Override
public Aead getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof ChaCha20Poly1305Key)) {
        throw new GeneralSecurityException("expected ChaCha20Poly1305Key proto");
    }
    ChaCha20Poly1305Key keyProto = (ChaCha20Poly1305Key) key;
    validate(keyProto);
    return new ChaCha20Poly1305(keyProto.getKeyValue().toByteArray());
}
Also used : ChaCha20Poly1305(com.google.crypto.tink.subtle.ChaCha20Poly1305) ChaCha20Poly1305Key(com.google.crypto.tink.proto.ChaCha20Poly1305Key) GeneralSecurityException(java.security.GeneralSecurityException)

Example 3 with ChaCha20Poly1305Key

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

the class ChaCha20Poly1305KeyManagerTest method createKey_values.

@Test
public void createKey_values() throws Exception {
    ChaCha20Poly1305Key key = manager.keyFactory().createKey(ChaCha20Poly1305KeyFormat.getDefaultInstance());
    assertThat(key.getVersion()).isEqualTo(0);
    assertThat(key.getKeyValue()).hasSize(32);
}
Also used : ChaCha20Poly1305Key(com.google.crypto.tink.proto.ChaCha20Poly1305Key) Test(org.junit.Test)

Aggregations

ChaCha20Poly1305Key (com.google.crypto.tink.proto.ChaCha20Poly1305Key)3 Test (org.junit.Test)2 KeyData (com.google.crypto.tink.proto.KeyData)1 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)1 ChaCha20Poly1305 (com.google.crypto.tink.subtle.ChaCha20Poly1305)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1