Skip to content

Commit 6e473a1

Browse files
Create individual request types for each AwsCrypto method
1 parent 59e3045 commit 6e473a1

17 files changed

+1065
-374
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
import java.util.Map;
2020

2121
import com.amazonaws.encryptionsdk.AwsCrypto;
22-
import com.amazonaws.encryptionsdk.AwsCrypto.AwsCryptoConfig;
2322
import com.amazonaws.encryptionsdk.AwsCryptoResult;
23+
import com.amazonaws.encryptionsdk.DecryptRequest;
24+
import com.amazonaws.encryptionsdk.EncryptRequest;
2425
import com.amazonaws.encryptionsdk.keyrings.Keyring;
2526
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings;
2627
import com.amazonaws.encryptionsdk.kms.KmsClientSupplier;
@@ -71,36 +72,37 @@ static void encryptAndDecrypt(final String keyArn) {
7172
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
7273
final Map<String, String> encryptionContext = Collections.singletonMap("ExampleContextKey", "ExampleContextValue");
7374

74-
// 5. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring and encryption context
75-
final AwsCryptoConfig config = AwsCryptoConfig.builder()
76-
.keyring(keyring)
77-
.encryptionContext(encryptionContext)
78-
.build();
79-
80-
// 6. Encrypt the data
81-
final AwsCryptoResult<byte[]> encryptResult = crypto.encryptData(config, EXAMPLE_DATA);
75+
// 5. Encrypt the data with the keyring and encryption context
76+
final AwsCryptoResult<byte[]> encryptResult = crypto.encrypt(
77+
EncryptRequest.builder()
78+
.keyring(keyring)
79+
.encryptionContext(encryptionContext)
80+
.plaintext(EXAMPLE_DATA).build());
8281
final byte[] ciphertext = encryptResult.getResult();
8382

84-
// 7. Decrypt the data. The same keyring may be used to encrypt and decrypt, but for decryption
83+
// 6. Decrypt the data. The same keyring may be used to encrypt and decrypt, but for decryption
8584
// the key IDs must be in the key ARN format.
86-
final AwsCryptoResult<byte[]> decryptResult = crypto.decryptData(config, ciphertext);
85+
final AwsCryptoResult<byte[]> decryptResult = crypto.decrypt(
86+
DecryptRequest.builder()
87+
.keyring(keyring)
88+
.ciphertext(ciphertext).build());
8789

88-
// 8. Before verifying the plaintext, inspect the Keyring Trace to verify that the CMK used
90+
// 7. Before verifying the plaintext, inspect the Keyring Trace to verify that the CMK used
8991
// to decrypt the encrypted data key was the CMK in the encryption keyring.
9092
if(!decryptResult.getKeyringTrace().getEntries().get(0).getKeyName().equals(keyArn)) {
9193
throw new IllegalStateException("Wrong key ID!");
9294
}
9395

94-
// 9. Also, verify that the encryption context in the result contains the
95-
// encryption context supplied to the encryptData method. Because the
96-
// SDK can add values to the encryption context, don't require that
97-
// the entire context matches.
96+
// 8. Also, verify that the encryption context in the result contains the
97+
// encryption context supplied to the encryptData method. Because the
98+
// SDK can add values to the encryption context, don't require that
99+
// the entire context matches.
98100
if (!encryptionContext.entrySet().stream()
99101
.allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) {
100102
throw new IllegalStateException("Wrong Encryption Context!");
101103
}
102104

103-
// 10. Verify that the decrypted plaintext matches the original plaintext
105+
// 9. Verify that the decrypted plaintext matches the original plaintext
104106
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);
105107
}
106108
}

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
package com.amazonaws.crypto.examples;
1515

1616
import com.amazonaws.encryptionsdk.AwsCrypto;
17-
import com.amazonaws.encryptionsdk.AwsCrypto.AwsCryptoConfig;
17+
import com.amazonaws.encryptionsdk.DecryptRequest;
18+
import com.amazonaws.encryptionsdk.EncryptRequest;
1819
import com.amazonaws.encryptionsdk.keyrings.Keyring;
1920
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings;
2021
import com.amazonaws.encryptionsdk.kms.KmsClientSupplier;
@@ -48,7 +49,7 @@
4849
* so that either key alone can decrypt it. You might commonly use the KMS CMK for decryption. However,
4950
* at any time, you can use the private RSA key to decrypt the ciphertext independent of KMS.
5051
*
51-
* This sample uses an RawRsaKeyring to generate a RSA public-private key pair
52+
* This sample uses a RawRsaKeyring to generate a RSA public-private key pair
5253
* and saves the key pair in memory. In practice, you would store the private key in a secure offline
5354
* location, such as an offline HSM, and distribute the public key to your development team.
5455
*
@@ -105,15 +106,13 @@ private static byte[] standardEncrypt(final String kmsArn, final PublicKey publi
105106
// 5. Combine the providers into a single MultiKeyring
106107
final Keyring keyring = StandardKeyrings.multi(kmsKeyring, rsaKeyring);
107108

108-
// 6. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring
109+
// 6. Encrypt the data with the keyring.
109110
// To simplify the code, we omit the encryption context. Production code should always
110111
// use an encryption context. For an example, see the other SDK samples.
111-
final AwsCryptoConfig config = AwsCryptoConfig.builder()
112+
return crypto.encrypt(EncryptRequest.builder()
112113
.keyring(keyring)
113-
.build();
114-
115-
// 7. Encrypt the data
116-
return crypto.encryptData(config, EXAMPLE_DATA).getResult();
114+
.plaintext(EXAMPLE_DATA).build())
115+
.getResult();
117116
}
118117

119118
private static byte[] standardDecrypt(final String kmsArn, final byte[] cipherText) {
@@ -131,15 +130,12 @@ private static byte[] standardDecrypt(final String kmsArn, final byte[] cipherTe
131130
// key with, but those can be supplied as necessary.
132131
final Keyring kmsKeyring = StandardKeyrings.kms(clientSupplier, emptyList(), emptyList(), kmsArn);
133132

134-
// 4. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring
133+
// 4. Decrypt the data with the keyring.
135134
// To simplify the code, we omit the encryption context. Production code should always
136135
// use an encryption context. For an example, see the other SDK samples.
137-
final AwsCryptoConfig config = AwsCryptoConfig.builder()
136+
return crypto.decrypt(DecryptRequest.builder()
138137
.keyring(kmsKeyring)
139-
.build();
140-
141-
// 5. Decrypt the data
142-
return crypto.decryptData(config, cipherText).getResult();
138+
.ciphertext(cipherText).build()).getResult();
143139
}
144140

145141
private static byte[] escrowDecrypt(final byte[] cipherText, final PublicKey publicEscrowKey, final PrivateKey privateEscrowKey) {
@@ -153,15 +149,12 @@ private static byte[] escrowDecrypt(final byte[] cipherText, final PublicKey pub
153149
final Keyring rsaKeyring = StandardKeyrings.rawRsa("Escrow", "Escrow",
154150
publicEscrowKey, privateEscrowKey, "RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
155151

156-
// 3. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring
152+
// 3. Decrypt the data with the keyring
157153
// To simplify the code, we omit the encryption context. Production code should always
158154
// use an encryption context. For an example, see the other SDK samples.
159-
final AwsCryptoConfig config = AwsCryptoConfig.builder()
155+
return crypto.decrypt(DecryptRequest.builder()
160156
.keyring(rsaKeyring)
161-
.build();
162-
163-
// 4. Decrypt the data
164-
return crypto.decryptData(config, cipherText).getResult();
157+
.ciphertext(cipherText).build()).getResult();
165158
}
166159

167160
private static KeyPair generateEscrowKeyPair() throws GeneralSecurityException {

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
package com.amazonaws.crypto.examples;
1515

1616
import com.amazonaws.encryptionsdk.AwsCrypto;
17-
import com.amazonaws.encryptionsdk.AwsCrypto.AwsCryptoConfig;
1817
import com.amazonaws.encryptionsdk.AwsCryptoInputStream;
18+
import com.amazonaws.encryptionsdk.CreateDecryptingInputStreamRequest;
19+
import com.amazonaws.encryptionsdk.CreateEncryptingInputStreamRequest;
1920
import com.amazonaws.encryptionsdk.keyrings.Keyring;
2021
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings;
2122
import com.amazonaws.util.IOUtils;
@@ -78,40 +79,40 @@ static void encryptAndDecrypt(final File srcFile, final File encryptedFile, fina
7879
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
7980
final Map<String, String> encryptionContext = Collections.singletonMap("Example", "FileStreaming");
8081

81-
// 5. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring and encryption context
82-
final AwsCryptoConfig config = AwsCryptoConfig.builder()
83-
.keyring(keyring)
84-
.encryptionContext(encryptionContext)
85-
.build();
86-
87-
// 6. Create the encrypting stream. Because the file might be too large to load into memory,
82+
// 5. Create the encrypting input stream with the keyring and encryption context.
83+
// Because the file might be too large to load into memory,
8884
// we stream the data, instead of loading it all at once.
89-
try (final AwsCryptoInputStream encryptingStream =
90-
crypto.createEncryptingStream(config, new FileInputStream(srcFile))) {
85+
try (final AwsCryptoInputStream encryptingStream = crypto.createEncryptingInputStream(
86+
CreateEncryptingInputStreamRequest.builder()
87+
.keyring(keyring)
88+
.encryptionContext(encryptionContext)
89+
.inputStream(new FileInputStream(srcFile)).build())) {
9190

92-
// 7. Copy the encrypted data into the encrypted file.
91+
// 6. Copy the encrypted data into the encrypted file.
9392
try (FileOutputStream out = new FileOutputStream(encryptedFile)) {
9493
IOUtils.copy(encryptingStream, out);
9594
}
9695
}
9796

98-
// 8. Create the decrypting stream.
99-
try(final AwsCryptoInputStream decryptingStream =
100-
crypto.createDecryptingStream(config, new FileInputStream(encryptedFile))) {
97+
// 7. Create the decrypting input stream with the keyring.
98+
try(final AwsCryptoInputStream decryptingStream = crypto.createDecryptingInputStream(
99+
CreateDecryptingInputStreamRequest.builder()
100+
.keyring(keyring)
101+
.inputStream(new FileInputStream(encryptedFile)).build())) {
101102

102-
// 9. Verify that the encryption context in the result contains the
103+
// 8. Verify that the encryption context in the result contains the
103104
// encryption context supplied to the createEncryptingStream method.
104105
if (!"FileStreaming".equals(decryptingStream.getAwsCryptoResult().getEncryptionContext().get("Example"))) {
105106
throw new IllegalStateException("Bad encryption context");
106107
}
107108

108-
// 10. Copy the plaintext data to a file
109+
// 9. Copy the plaintext data to a file
109110
try (FileOutputStream out = new FileOutputStream(decryptedFile)) {
110111
IOUtils.copy(decryptingStream, out);
111112
}
112113
}
113114

114-
// 11. Compare the decrypted file to the original
115+
// 10. Compare the decrypted file to the original
115116
compareFiles(decryptedFile, srcFile);
116117
}
117118

0 commit comments

Comments
 (0)