Skip to content

Commit d444f52

Browse files
Using try-with-resources for AwsCrypto streams
1 parent 63ff149 commit d444f52

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

src/examples/java/com/amazonaws/crypto/examples/EscrowedEncryptExample.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.amazonaws.crypto.examples;
1515

1616
import com.amazonaws.encryptionsdk.AwsCrypto;
17+
import com.amazonaws.encryptionsdk.AwsCrypto.AwsCryptoConfig;
1718
import com.amazonaws.encryptionsdk.keyrings.Keyring;
1819
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings;
1920
import com.amazonaws.encryptionsdk.kms.KmsClientSupplier;
@@ -107,7 +108,7 @@ private static byte[] standardEncrypt(final String kmsArn, final PublicKey publi
107108
// 6. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring
108109
// To simplify the code, we omit the encryption context. Production code should always
109110
// use an encryption context. For an example, see the other SDK samples.
110-
final AwsCrypto.AwsCryptoConfig config = AwsCrypto.AwsCryptoConfig.builder()
111+
final AwsCryptoConfig config = AwsCryptoConfig.builder()
111112
.keyring(keyring)
112113
.build();
113114

@@ -133,7 +134,7 @@ private static byte[] standardDecrypt(final String kmsArn, final byte[] cipherTe
133134
// 4. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring
134135
// To simplify the code, we omit the encryption context. Production code should always
135136
// use an encryption context. For an example, see the other SDK samples.
136-
final AwsCrypto.AwsCryptoConfig config = AwsCrypto.AwsCryptoConfig.builder()
137+
final AwsCryptoConfig config = AwsCryptoConfig.builder()
137138
.keyring(kmsKeyring)
138139
.build();
139140

@@ -155,7 +156,7 @@ private static byte[] escrowDecrypt(final byte[] cipherText, final PublicKey pub
155156
// 3. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring
156157
// To simplify the code, we omit the encryption context. Production code should always
157158
// use an encryption context. For an example, see the other SDK samples.
158-
final AwsCrypto.AwsCryptoConfig config = AwsCrypto.AwsCryptoConfig.builder()
159+
final AwsCryptoConfig config = AwsCryptoConfig.builder()
159160
.keyring(rsaKeyring)
160161
.build();
161162

src/examples/java/com/amazonaws/crypto/examples/FileStreamingExample.java

+26-29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.amazonaws.crypto.examples;
1515

1616
import com.amazonaws.encryptionsdk.AwsCrypto;
17+
import com.amazonaws.encryptionsdk.AwsCrypto.AwsCryptoConfig;
1718
import com.amazonaws.encryptionsdk.AwsCryptoInputStream;
1819
import com.amazonaws.encryptionsdk.keyrings.Keyring;
1920
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings;
@@ -49,13 +50,15 @@
4950
public class FileStreamingExample {
5051

5152
public static void main(String[] args) throws IOException {
52-
final String srcFile = args[0];
53+
final File srcFile = new File(args[0]);
54+
final File encryptedFile = new File(args[1]);
55+
final File decryptedFile = new File(args[2]);
5356

54-
encryptAndDecrypt(srcFile);
57+
encryptAndDecrypt(srcFile, encryptedFile, decryptedFile);
5558

5659
}
5760

58-
static void encryptAndDecrypt(final String srcFile) throws IOException {
61+
static void encryptAndDecrypt(final File srcFile, final File encryptedFile, final File decryptedFile) throws IOException {
5962
// 1. Instantiate the SDK
6063
final AwsCrypto crypto = new AwsCrypto();
6164

@@ -76,46 +79,40 @@ static void encryptAndDecrypt(final String srcFile) throws IOException {
7679
final Map<String, String> encryptionContext = Collections.singletonMap("Example", "FileStreaming");
7780

7881
// 5. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring and encryption context
79-
final AwsCrypto.AwsCryptoConfig config = AwsCrypto.AwsCryptoConfig.builder()
82+
final AwsCryptoConfig config = AwsCryptoConfig.builder()
8083
.keyring(keyring)
8184
.encryptionContext(encryptionContext)
8285
.build();
8386

8487
// 6. Create the encrypting stream. Because the file might be too large to load into memory,
8588
// we stream the data, instead of loading it all at once.
86-
final AwsCryptoInputStream encryptingStream =
87-
crypto.createEncryptingStream(config, new FileInputStream(srcFile));
88-
89-
// 7. Copy the encrypted data into a file.
90-
final File encryptedFile = new File(srcFile + ".encrypted");
91-
try (FileOutputStream out = new FileOutputStream(encryptedFile)) {
92-
IOUtils.copy(encryptingStream, out);
93-
encryptingStream.close();
89+
try (final AwsCryptoInputStream encryptingStream =
90+
crypto.createEncryptingStream(config, new FileInputStream(srcFile))) {
91+
92+
// 7. Copy the encrypted data into the encrypted file.
93+
try (FileOutputStream out = new FileOutputStream(encryptedFile)) {
94+
IOUtils.copy(encryptingStream, out);
95+
}
9496
}
9597

9698
// 8. Create the decrypting stream.
97-
final AwsCryptoInputStream decryptingStream =
98-
crypto.createDecryptingStream(config, new FileInputStream(encryptedFile));
99+
try(final AwsCryptoInputStream decryptingStream =
100+
crypto.createDecryptingStream(config, new FileInputStream(encryptedFile))) {
99101

100-
// 9. Verify that the encryption context in the result contains the
101-
// encryption context supplied to the createEncryptingStream method.
102-
if (!"FileStreaming".equals(decryptingStream.getAwsCryptoResult().getEncryptionContext().get("Example"))) {
103-
throw new IllegalStateException("Bad encryption context");
104-
}
102+
// 9. Verify that the encryption context in the result contains the
103+
// encryption context supplied to the createEncryptingStream method.
104+
if (!"FileStreaming".equals(decryptingStream.getAwsCryptoResult().getEncryptionContext().get("Example"))) {
105+
throw new IllegalStateException("Bad encryption context");
106+
}
105107

106-
// 10. Copy the plaintext data to a file
107-
final File decryptedFile = new File(srcFile + ".decrypted");
108-
try (FileOutputStream out = new FileOutputStream(decryptedFile)) {
109-
IOUtils.copy(decryptingStream, out);
110-
decryptingStream.close();
108+
// 10. Copy the plaintext data to a file
109+
try (FileOutputStream out = new FileOutputStream(decryptedFile)) {
110+
IOUtils.copy(decryptingStream, out);
111+
}
111112
}
112113

113114
// 11. Compare the decrypted file to the original
114-
compareFiles(decryptedFile, new File(srcFile));
115-
116-
// 12. Clean up the encrypted and decrypted files
117-
encryptedFile.delete();
118-
decryptedFile.delete();
115+
compareFiles(decryptedFile, srcFile);
119116
}
120117

121118
/**

src/test/java/com/amazonaws/crypto/examples/FileStreamingExampleTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ class FileStreamingExampleTest {
2626
@Test
2727
void testEncryptAndDecrypt() throws IOException {
2828
final File tempFile = File.createTempFile("FileStreamingExampleTest-TempTestData", ".tmp");
29+
final File encryptedFile = new File(tempFile.getPath() + ".encrypted");
30+
final File decryptedFile = new File(tempFile.getPath() + ".decrypted");
2931
tempFile.deleteOnExit();
32+
encryptedFile.deleteOnExit();
33+
decryptedFile.deleteOnExit();
3034

3135
try(BufferedWriter writer = Files.newBufferedWriter(tempFile.toPath())) {
3236
for(int i = 0; i < 1000 ; i++) {
@@ -35,6 +39,6 @@ void testEncryptAndDecrypt() throws IOException {
3539
}
3640
}
3741

38-
FileStreamingExample.encryptAndDecrypt(tempFile.getPath());
42+
FileStreamingExample.encryptAndDecrypt(tempFile, encryptedFile, decryptedFile);
3943
}
4044
}

0 commit comments

Comments
 (0)