Search in sources :

Example 1 with XChaCha20Poly1305Key

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

the class XChaCha20Poly1305KeyManagerTest method testDeriveKey.

@Test
public void testDeriveKey() throws Exception {
    final int keySize = 32;
    byte[] keyMaterial = Random.randBytes(100);
    XChaCha20Poly1305Key key = factory.deriveKey(XChaCha20Poly1305KeyFormat.newBuilder().setVersion(0).build(), new ByteArrayInputStream(keyMaterial));
    assertThat(key.getKeyValue()).hasSize(keySize);
    for (int i = 0; i < keySize; ++i) {
        assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XChaCha20Poly1305Key(com.google.crypto.tink.proto.XChaCha20Poly1305Key) Test(org.junit.Test)

Example 2 with XChaCha20Poly1305Key

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

the class XChaCha20Poly1305KeyManagerTest method getPrimitive.

@Test
public void getPrimitive() throws Exception {
    XChaCha20Poly1305Key key = factory.createKey(XChaCha20Poly1305KeyFormat.getDefaultInstance());
    Aead managerAead = manager.getPrimitive(key, Aead.class);
    Aead directAead = new XChaCha20Poly1305(key.getKeyValue().toByteArray());
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    assertThat(directAead.decrypt(managerAead.encrypt(plaintext, associatedData), associatedData)).isEqualTo(plaintext);
}
Also used : Aead(com.google.crypto.tink.Aead) XChaCha20Poly1305Key(com.google.crypto.tink.proto.XChaCha20Poly1305Key) XChaCha20Poly1305(com.google.crypto.tink.subtle.XChaCha20Poly1305) Test(org.junit.Test)

Aggregations

XChaCha20Poly1305Key (com.google.crypto.tink.proto.XChaCha20Poly1305Key)2 Test (org.junit.Test)2 Aead (com.google.crypto.tink.Aead)1 XChaCha20Poly1305 (com.google.crypto.tink.subtle.XChaCha20Poly1305)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1