aboutsummaryrefslogtreecommitdiff
path: root/java_src/examples
diff options
context:
space:
mode:
authorjuerg <juerg@google.com>2022-10-20 00:45:32 -0700
committerCopybara-Service <copybara-worker@google.com>2022-10-20 00:47:01 -0700
commit098344a67bef3ee16f8f53c250b9a714b6ff95b4 (patch)
treec484934abf2ea309042e143eb87783491c67c7f2 /java_src/examples
parentb104885f2e564f92decd8114c5a0cf158f18b271 (diff)
downloadtink-098344a67bef3ee16f8f53c250b9a714b6ff95b4.tar.gz
Use JsonKeysetReader and JsonKeysetWriter only with streams, and not with files.
PiperOrigin-RevId: 482410037
Diffstat (limited to 'java_src/examples')
-rw-r--r--java_src/examples/cleartextkeyset/CleartextKeysetExample.java9
-rw-r--r--java_src/examples/encryptedkeyset/EncryptedKeysetExample.java9
-rw-r--r--java_src/examples/helloworld/src/main/java/com/helloworld/Commands.java9
3 files changed, 19 insertions, 8 deletions
diff --git a/java_src/examples/cleartextkeyset/CleartextKeysetExample.java b/java_src/examples/cleartextkeyset/CleartextKeysetExample.java
index a36a2ab66..f9760a191 100644
--- a/java_src/examples/cleartextkeyset/CleartextKeysetExample.java
+++ b/java_src/examples/cleartextkeyset/CleartextKeysetExample.java
@@ -22,6 +22,7 @@ import com.google.crypto.tink.KeyTemplates;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.aead.AeadConfig;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
@@ -73,7 +74,9 @@ public final class CleartextKeysetExample {
// [END generate-a-new-keyset]
// [START store-a-cleartext-keyset]
- CleartextKeysetHandle.write(handle, JsonKeysetWriter.withFile(keyFile));
+ try (FileOutputStream outputStream = new FileOutputStream(keyFile)) {
+ CleartextKeysetHandle.write(handle, JsonKeysetWriter.withOutputStream(outputStream));
+ }
// [END store-a-cleartext-keyset]
System.exit(0);
}
@@ -82,8 +85,8 @@ public final class CleartextKeysetExample {
// Read the cleartext keyset
KeysetHandle handle = null;
- try {
- handle = CleartextKeysetHandle.read(JsonKeysetReader.withFile(keyFile));
+ try (FileInputStream inputStream = new FileInputStream(keyFile)) {
+ handle = CleartextKeysetHandle.read(JsonKeysetReader.withInputStream(inputStream));
} catch (GeneralSecurityException | IOException ex) {
System.err.println("Error reading key: " + ex);
System.exit(1);
diff --git a/java_src/examples/encryptedkeyset/EncryptedKeysetExample.java b/java_src/examples/encryptedkeyset/EncryptedKeysetExample.java
index d6e704cff..b7f5e1be6 100644
--- a/java_src/examples/encryptedkeyset/EncryptedKeysetExample.java
+++ b/java_src/examples/encryptedkeyset/EncryptedKeysetExample.java
@@ -23,6 +23,7 @@ import com.google.crypto.tink.aead.AeadConfig;
import com.google.crypto.tink.aead.KmsAeadKeyManager;
import com.google.crypto.tink.integration.gcpkms.GcpKmsClient;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
@@ -96,7 +97,9 @@ public final class EncryptedKeysetExample {
// [END generate-a-new-keyset]
// [START encrypt-a-keyset]
- handle.write(JsonKeysetWriter.withFile(keyFile), kekAead);
+ try (FileOutputStream outputStream = new FileOutputStream(keyFile)) {
+ handle.write(JsonKeysetWriter.withOutputStream(outputStream), kekAead);
+ }
// [END encrypt-a-keyset]
System.exit(0);
}
@@ -105,8 +108,8 @@ public final class EncryptedKeysetExample {
// Read the encrypted keyset
KeysetHandle handle = null;
- try {
- handle = KeysetHandle.read(JsonKeysetReader.withFile(keyFile), kekAead);
+ try (FileInputStream inputStream = new FileInputStream(keyFile)) {
+ handle = KeysetHandle.read(JsonKeysetReader.withInputStream(inputStream), kekAead);
} catch (GeneralSecurityException | IOException ex) {
System.err.println("Error reading key: " + ex);
System.exit(1);
diff --git a/java_src/examples/helloworld/src/main/java/com/helloworld/Commands.java b/java_src/examples/helloworld/src/main/java/com/helloworld/Commands.java
index 05f5a711d..f036e36af 100644
--- a/java_src/examples/helloworld/src/main/java/com/helloworld/Commands.java
+++ b/java_src/examples/helloworld/src/main/java/com/helloworld/Commands.java
@@ -21,6 +21,7 @@ import com.google.crypto.tink.JsonKeysetWriter;
import com.google.crypto.tink.KeyTemplates;
import com.google.crypto.tink.KeysetHandle;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
@@ -61,10 +62,14 @@ public final class Commands {
// WARNING: reading cleartext keysets is a bad practice. Tink supports reading/writing
// encrypted keysets, see
// https://github.com/google/tink/blob/master/docs/JAVA-HOWTO.md#loading-existing-keysets.
- return CleartextKeysetHandle.read(JsonKeysetReader.withFile(keyset));
+ try (FileInputStream inputStream = new FileInputStream(keyset)) {
+ return CleartextKeysetHandle.read(JsonKeysetReader.withInputStream(inputStream));
+ }
}
KeysetHandle handle = KeysetHandle.generateNew(KeyTemplates.get("AES128_GCM"));
- CleartextKeysetHandle.write(handle, JsonKeysetWriter.withFile(keyset));
+ try (FileOutputStream outputStream = new FileOutputStream(keyset)) {
+ CleartextKeysetHandle.write(handle, JsonKeysetWriter.withOutputStream(outputStream));
+ }
return handle;
}