Skip to content

Commit 6081eba

Browse files
juergwcopybara-github
authored andcommitted
Update HelloWorld example.
- Don't register KMS client. - Rename parameter to "keyset encryption key uri". - Remove "encrypt" and "decrypt" functions. They create a new keyset handle every time they are called which should be avoided. - The "getKeysetHandle" function creates a new keyset if there is non, and loads it otherwise. I think this is bad pattern we should avoid. In this example, I think it's better to just fail if the keyset exists, and have two functions that do these two steps. PiperOrigin-RevId: 560686600 Change-Id: I57256693530d1bd1c98172074e16182859b658fb
1 parent 09f004a commit 6081eba

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

examples/maven/src/main/java/com/helloworld/HelloWorld.java

+28-29
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.google.crypto.tink.Aead;
2020
import com.google.crypto.tink.KeysetHandle;
21-
import com.google.crypto.tink.KmsClients;
21+
import com.google.crypto.tink.KmsClient;
2222
import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
2323
import com.google.crypto.tink.aead.AeadConfig;
2424
import com.google.crypto.tink.aead.PredefinedAeadParameters;
@@ -29,7 +29,6 @@
2929
import java.nio.file.Paths;
3030
import java.security.GeneralSecurityException;
3131
import java.util.Arrays;
32-
import java.util.Optional;
3332

3433
/**
3534
* Encrypts a string
@@ -44,36 +43,25 @@ public final class HelloWorld {
4443

4544
private static void usage() {
4645
System.out.println(
47-
"Usage: mvn exec:java -Dexec.args=\"<keyset file> <credentials path> <master key uri>\"");
46+
"Usage: mvn exec:java -Dexec.args=\""
47+
+ "<keyset file> <credentials path> <keyset encryption key uri>\"");
4848
}
4949

50-
/** Loads a KeysetHandle from {@code keyset} or generate a new one if it doesn't exist. */
51-
private static KeysetHandle getKeysetHandle(Path keysetPath, String masterKeyUri)
50+
/** Creates a new keyset with one AEAD key, and write it encrypted to disk. */
51+
private static void createAndWriteEncryptedKeyset(Path keysetPath, Aead keysetEncryptionAead)
5252
throws GeneralSecurityException, IOException {
53-
Aead masterKeyAead = KmsClients.get(masterKeyUri).getAead(masterKeyUri);
54-
if (Files.exists(keysetPath)) {
55-
return TinkJsonProtoKeysetFormat.parseEncryptedKeyset(
56-
new String(Files.readAllBytes(keysetPath), UTF_8), masterKeyAead, new byte[0]);
57-
}
5853
KeysetHandle handle = KeysetHandle.generateNew(PredefinedAeadParameters.AES128_GCM);
5954
String serializedEncryptedKeyset =
60-
TinkJsonProtoKeysetFormat.serializeEncryptedKeyset(handle, masterKeyAead, new byte[0]);
55+
TinkJsonProtoKeysetFormat.serializeEncryptedKeyset(
56+
handle, keysetEncryptionAead, new byte[0]);
6157
Files.write(keysetPath, serializedEncryptedKeyset.getBytes(UTF_8));
62-
return handle;
6358
}
6459

65-
private static byte[] encrypt(Path keyset, String masterKeyUri, byte[] plaintext)
66-
throws Exception {
67-
KeysetHandle keysetHandle = getKeysetHandle(keyset, masterKeyUri);
68-
Aead aead = keysetHandle.getPrimitive(Aead.class);
69-
return aead.encrypt(plaintext, associatedData);
70-
}
71-
72-
private static byte[] decrypt(Path keyset, String masterKeyUri, byte[] ciphertext)
73-
throws Exception {
74-
KeysetHandle keysetHandle = getKeysetHandle(keyset, masterKeyUri);
75-
Aead aead = keysetHandle.getPrimitive(Aead.class);
76-
return aead.decrypt(ciphertext, associatedData);
60+
/** Reads an encrypted keyset from disk. */
61+
private static KeysetHandle readEncryptedKeyset(Path keysetPath, Aead keysetEncryptionAead)
62+
throws GeneralSecurityException, IOException {
63+
return TinkJsonProtoKeysetFormat.parseEncryptedKeyset(
64+
new String(Files.readAllBytes(keysetPath), UTF_8), keysetEncryptionAead, new byte[0]);
7765
}
7866

7967
public static void main(String[] args) throws Exception {
@@ -82,17 +70,28 @@ public static void main(String[] args) throws Exception {
8270
System.exit(1);
8371
}
8472

85-
Path keysetFile = Paths.get(args[0]);
73+
Path keysetPath = Paths.get(args[0]);
8674
Path credentialsPath = Paths.get(args[1]);
87-
String masterKeyUri = args[2];
75+
String keysetEncryptionKeyUri = args[2];
8876

8977
// Register all AEAD key types with the Tink runtime.
9078
AeadConfig.register();
91-
AwsKmsClient.register(Optional.of(masterKeyUri), Optional.of(credentialsPath.toString()));
9279

93-
byte[] ciphertext = encrypt(keysetFile, masterKeyUri, plaintext);
94-
byte[] decrypted = decrypt(keysetFile, masterKeyUri, ciphertext);
80+
KmsClient client = new AwsKmsClient().withCredentials(credentialsPath.toString());
81+
Aead keysetEncryptionAead = client.getAead(keysetEncryptionKeyUri);
82+
83+
if (Files.exists(keysetPath)) {
84+
System.out.println("keyset file already exists");
85+
System.exit(1);
86+
}
87+
88+
createAndWriteEncryptedKeyset(keysetPath, keysetEncryptionAead);
89+
90+
KeysetHandle keysetHandle = readEncryptedKeyset(keysetPath, keysetEncryptionAead);
91+
Aead aead = keysetHandle.getPrimitive(Aead.class);
9592

93+
byte[] ciphertext = aead.encrypt(plaintext, associatedData);
94+
byte[] decrypted = aead.decrypt(ciphertext, associatedData);
9695
if (!Arrays.equals(decrypted, plaintext)) {
9796
System.out.println("Decryption failed");
9897
System.exit(1);

0 commit comments

Comments
 (0)