From 7e4426207f60a6657a3a8fd1672cd8fbe01334c4 Mon Sep 17 00:00:00 2001 From: Thai Duong Date: Wed, 18 Apr 2018 16:34:19 -0700 Subject: [PATCH] Updating the Java HOWTO to include sample code for DeterministicAead. PiperOrigin-RevId: 193437447 GitOrigin-RevId: f9e0c3d9f88297cca3de81120836c9fc88020c6e --- doc/JAVA-HOWTO.md | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/doc/JAVA-HOWTO.md b/doc/JAVA-HOWTO.md index 4a4eb434fa..d7d4d35ace 100644 --- a/doc/JAVA-HOWTO.md +++ b/doc/JAVA-HOWTO.md @@ -281,9 +281,8 @@ factory offers corresponding `getPrimitive(...)` methods. ### Symmetric Key Encryption Here is how you can obtain and use an [AEAD (Authenticated Encryption with -Associated -Data](PRIMITIVES.md#authenticated-encryption-with-associated-data) primitive -to encrypt or decrypt data: +Associated Data](PRIMITIVES.md#authenticated-encryption-with-associated-data) +primitive to encrypt or decrypt data: ```java import com.google.crypto.tink.Aead; @@ -305,6 +304,34 @@ to encrypt or decrypt data: byte[] decrypted = aead.decrypt(ciphertext, aad); ``` +### Deterministic Symmetric Key Encryption + +Here is how you can obtain and use an [DeterministicAEAD (Deterministic +Authenticated Encryption with Associated +Data](PRIMITIVES.md#deterministic-authenticated-encryption-with-associated-data) +primitive to encrypt or decrypt data: + +```java + import com.google.crypto.tink.DeterministicAead; + import com.google.crypto.tink.KeysetHandle; + import com.google.crypto.tink.daead.DeterministicAeadFactory; + import com.google.crypto.tink.daead.DeterministicAeadKeyTemplates; + + // 1. Generate the key material. + KeysetHandle keysetHandle = KeysetHandle.generateNew( + DeterministicAeadKeyTemplates.AES256_SIV); + + // 2. Get the primitive. + DeterministicAead daead = + DeterministicAeadFactory.getPrimitive(keysetHandle); + + // 3. Use the primitive to deterministically encrypt a plaintext, + byte[] ciphertext = daead.encryptDeterministically(plaintext, aad); + + // ... or to deterministically decrypt a ciphertext. + byte[] decrypted = daead.decryptDeterministically(ciphertext, aad); +``` + ### Symmetric Key Encryption of Streaming Data Here is how you can obtain and use an [Streaming AEAD (Streaming Authenticated Encryption with