• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package walkthrough;
15 
16 import static com.google.common.truth.Truth.assertThat;
17 import static java.nio.charset.StandardCharsets.UTF_8;
18 
19 import com.google.crypto.tink.InsecureSecretKeyAccess;
20 import com.google.crypto.tink.KeysetHandle;
21 import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
22 import com.google.crypto.tink.aead.AeadConfig;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 @RunWith(JUnit4.class)
28 public final class ObtainAndUseAeadPrimitiveExampleTest {
29 
30   private static final String SERIALIZED_KEYSET =
31       "{"
32           + "  \"key\": ["
33           + "    {"
34           + "      \"keyData\": {"
35           + "        \"keyMaterialType\": \"SYMMETRIC\","
36           + "        \"typeUrl\": \"type.googleapis.com/google.crypto.tink.AesGcmKey\","
37           + "        \"value\": \"GiBWyUfGgYk3RTRhj/LIUzSudIWlyjCftCOypTr0jCNSLg==\""
38           + "      },"
39           + "      \"keyId\": 294406504,"
40           + "      \"outputPrefixType\": \"TINK\","
41           + "      \"status\": \"ENABLED\""
42           + "    }"
43           + "  ],"
44           + "  \"primaryKeyId\": 294406504"
45           + "}";
46 
47   @Test
encryptDecrypt_succeeds()48   public void encryptDecrypt_succeeds() throws Exception {
49     AeadConfig.register();
50     KeysetHandle keysetHandle =
51         TinkJsonProtoKeysetFormat.parseKeyset(SERIALIZED_KEYSET, InsecureSecretKeyAccess.get());
52     byte[] plaintext = "plaintext".getBytes(UTF_8);
53     byte[] associatedData = "associatedData".getBytes(UTF_8);
54 
55     byte[] result = ObtainAndUseAeadPrimitiveExample.aeadEncryptDecrypt(
56                 keysetHandle, plaintext, associatedData);
57 
58     assertThat(result).isEqualTo(plaintext);
59   }
60 }
61