Skip to content

Commit 7ec91d6

Browse files
Adding convenience methods the create builders internally
1 parent f415b7f commit 7ec91d6

File tree

5 files changed

+123
-9
lines changed

5 files changed

+123
-9
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static byte[] standardEncrypt(final AwsKmsCmkId kmsArn, final PublicKey
8989

9090
// 3. Instantiate a RawRsaKeyring
9191
// Because the user does not have access to the private escrow key,
92-
// they pass in "null" for the private key parameter.
92+
// they do not provide the private key parameter.
9393
final Keyring rsaKeyring = StandardKeyrings.rawRsa()
9494
.keyNamespace("Escrow")
9595
.keyName("Escrow")

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static void encryptAndDecrypt(final File srcFile, final File encryptedFile, fina
9999
}
100100

101101
// 7. Create the decrypting input stream with the keyring.
102-
try(final AwsCryptoInputStream decryptingStream = crypto.createDecryptingInputStream(
102+
try (final AwsCryptoInputStream decryptingStream = crypto.createDecryptingInputStream(
103103
CreateDecryptingInputStreamRequest.builder()
104104
.keyring(keyring)
105105
.inputStream(new FileInputStream(encryptedFile)).build())) {
@@ -143,7 +143,6 @@ private static void compareFiles(File file1, File file2) throws IOException {
143143
(file2Line = file2Reader.readLine()) != null) {
144144
assert Objects.equals(file1Line, file2Line);
145145
}
146-
147146
}
148147
}
149148

src/main/java/com/amazonaws/encryptionsdk/AwsCrypto.java

+107
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.nio.charset.StandardCharsets;
1919
import java.util.Collections;
2020
import java.util.Map;
21+
import java.util.function.Consumer;
2122

2223
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
2324
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
@@ -255,6 +256,21 @@ public long estimateCiphertextSize(final EstimateCiphertextSizeRequest request)
255256
return cryptoHandler.estimateOutputSize(request.plaintextSize());
256257
}
257258

259+
/**
260+
* Returns the best estimate for the output length of encrypting a plaintext with the plaintext size specified in
261+
* the provided {@link EstimateCiphertextSizeRequest}. The actual ciphertext may be shorter.
262+
* <p>
263+
* This is a convenience which creates an instance of the {@link EstimateCiphertextSizeRequest.Builder} avoiding the need to
264+
* create one manually via {@link EstimateCiphertextSizeRequest#builder()}
265+
* </p>
266+
*
267+
* @param request A Consumer that will call methods on EstimateCiphertextSizeRequest.Builder to create a request.
268+
* @return The estimated output length in bytes
269+
*/
270+
public long estimateCiphertextSize(final Consumer<EstimateCiphertextSizeRequest.Builder> request) {
271+
return estimateCiphertextSize(EstimateCiphertextSizeRequest.builder().applyMutation(request).build());
272+
}
273+
258274
/**
259275
* Returns an encrypted form of {@code plaintext} that has been protected with {@link DataKey}s
260276
* that are in turn protected by {@link MasterKey MasterKeys} provided by
@@ -355,6 +371,21 @@ public AwsCryptoResult<byte[]> encrypt(final EncryptRequest request) {
355371
cryptoHandler.getMasterKeys(), cryptoHandler.getHeaders());
356372
}
357373

374+
/**
375+
* Returns an encrypted form of {@code plaintext} that has been protected with either the {@link CryptoMaterialsManager}
376+
* or the {@link Keyring} specified in the {@link EncryptRequest}.
377+
* <p>
378+
* This is a convenience which creates an instance of the {@link EncryptRequest.Builder} avoiding the need to
379+
* create one manually via {@link EncryptRequest#builder()}
380+
* </p>
381+
*
382+
* @param request A Consumer that will call methods on EncryptRequest.Builder to create a request.
383+
* @return An {@link AwsCryptoResult} containing the encrypted data
384+
*/
385+
public AwsCryptoResult<byte[]> encrypt(final Consumer<EncryptRequest.Builder> request) {
386+
return encrypt(EncryptRequest.builder().applyMutation(request).build());
387+
}
388+
358389
/**
359390
* Calls {@link #encryptData(MasterKeyProvider, byte[], Map)} on the UTF-8 encoded bytes of
360391
* {@code plaintext} and base64 encodes the result.
@@ -525,6 +556,21 @@ public AwsCryptoResult<byte[]> decrypt(final DecryptRequest request) {
525556
cryptoHandler.getMasterKeys(), cryptoHandler.getHeaders());
526557
}
527558

559+
/**
560+
* Decrypts the provided {@link ParsedCiphertext} using the {@link CryptoMaterialsManager} or the {@link Keyring}
561+
* specified in the {@link DecryptRequest}.
562+
* <p>
563+
* This is a convenience which creates an instance of the {@link DecryptRequest.Builder} avoiding the need to
564+
* create one manually via {@link DecryptRequest#builder()}
565+
* </p>
566+
*
567+
* @param request A Consumer that will call methods on DecryptRequest.Builder to create a request.
568+
* @return An {@link AwsCryptoResult} containing the decrypted data
569+
*/
570+
public AwsCryptoResult<byte[]> decrypt(final Consumer<DecryptRequest.Builder> request) {
571+
return decrypt(DecryptRequest.builder().applyMutation(request).build());
572+
}
573+
528574
/**
529575
* Base64 decodes the {@code ciphertext} prior to decryption and then treats the results as a
530576
* UTF-8 encoded string.
@@ -658,6 +704,21 @@ public AwsCryptoOutputStream createEncryptingOutputStream(final CreateEncrypting
658704
request.cryptoMaterialsManager(), request.encryptionContext()));
659705
}
660706

707+
/**
708+
* Returns a {@link AwsCryptoOutputStream} which encrypts the data prior to passing it onto the
709+
* underlying {@link OutputStream}.
710+
* <p>
711+
* This is a convenience which creates an instance of the {@link CreateEncryptingOutputStreamRequest.Builder} avoiding the need to
712+
* create one manually via {@link CreateEncryptingOutputStreamRequest#builder()}
713+
* </p>
714+
*
715+
* @see #encrypt(EncryptRequest)
716+
* @see javax.crypto.CipherOutputStream
717+
*/
718+
public AwsCryptoOutputStream createEncryptingOutputStream(final Consumer<CreateEncryptingOutputStreamRequest.Builder> request) {
719+
return createEncryptingOutputStream(CreateEncryptingOutputStreamRequest.builder().applyMutation(request).build());
720+
}
721+
661722
/**
662723
* Returns a {@link CryptoInputStream} which encrypts the data after reading it from the
663724
* underlying {@link InputStream}.
@@ -748,6 +809,21 @@ public AwsCryptoInputStream createEncryptingInputStream(final CreateEncryptingIn
748809
return new AwsCryptoInputStream(request.inputStream(), cryptoHandler);
749810
}
750811

812+
/**
813+
* Returns a {@link AwsCryptoInputStream} which encrypts the data after reading it from the
814+
* underlying {@link InputStream}.
815+
* <p>
816+
* This is a convenience which creates an instance of the {@link CreateEncryptingInputStreamRequest.Builder} avoiding the need to
817+
* create one manually via {@link CreateEncryptingInputStreamRequest#builder()}
818+
* </p>
819+
*
820+
* @see #encrypt(EncryptRequest)
821+
* @see javax.crypto.CipherInputStream
822+
*/
823+
public AwsCryptoInputStream createEncryptingInputStream(final Consumer<CreateEncryptingInputStreamRequest.Builder> request) {
824+
return createEncryptingInputStream(CreateEncryptingInputStreamRequest.builder().applyMutation(request).build());
825+
}
826+
751827
/**
752828
* Returns a {@link CryptoOutputStream} which decrypts the data prior to passing it onto the
753829
* underlying {@link OutputStream}.
@@ -812,6 +888,22 @@ public AwsCryptoOutputStream createDecryptingOutputStream(final CreateDecrypting
812888
return new AwsCryptoOutputStream(request.outputStream(), cryptoHandler);
813889
}
814890

891+
892+
/**
893+
* Returns a {@link AwsCryptoOutputStream} which decrypts the data prior to passing it onto the
894+
* underlying {@link OutputStream}.
895+
* <p>
896+
* This is a convenience which creates an instance of the {@link CreateDecryptingOutputStreamRequest.Builder} avoiding the need to
897+
* create one manually via {@link CreateDecryptingOutputStreamRequest#builder()}
898+
* </p>
899+
*
900+
* @see #decrypt(DecryptRequest)
901+
* @see javax.crypto.CipherOutputStream
902+
*/
903+
public AwsCryptoOutputStream createDecryptingOutputStream(final Consumer<CreateDecryptingOutputStreamRequest.Builder> request) {
904+
return createDecryptingOutputStream(CreateDecryptingOutputStreamRequest.builder().applyMutation(request).build());
905+
}
906+
815907
/**
816908
* Returns a {@link CryptoInputStream} which decrypts the data after reading it from the
817909
* underlying {@link InputStream}.
@@ -844,6 +936,21 @@ public AwsCryptoInputStream createDecryptingInputStream(final CreateDecryptingIn
844936
return new AwsCryptoInputStream(request.inputStream(), cryptoHandler);
845937
}
846938

939+
/**
940+
* Returns a {@link AwsCryptoInputStream} which decrypts the data after reading it from the
941+
* underlying {@link InputStream}.
942+
* <p>
943+
* This is a convenience which creates an instance of the {@link CreateDecryptingInputStreamRequest.Builder} avoiding the need to
944+
* create one manually via {@link CreateDecryptingInputStreamRequest#builder()}
945+
* </p>
946+
*
947+
* @see #encrypt(EncryptRequest)
948+
* @see javax.crypto.CipherInputStream
949+
*/
950+
public AwsCryptoInputStream createDecryptingInputStream(final Consumer<CreateDecryptingInputStreamRequest.Builder> request) {
951+
return createDecryptingInputStream(CreateDecryptingInputStreamRequest.builder().applyMutation(request).build());
952+
}
953+
847954
private MessageCryptoHandler getEncryptingStreamHandler(
848955
CryptoMaterialsManager materialsManager, Map<String, String> encryptionContext
849956
) {

src/main/java/com/amazonaws/encryptionsdk/AwsCryptoRequest.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
* specific language governing permissions and limitations under the License.
1212
*/
1313

14-
package com.amazonaws.encryptionsdk;import com.amazonaws.encryptionsdk.keyrings.Keyring;
14+
package com.amazonaws.encryptionsdk;
15+
16+
import com.amazonaws.encryptionsdk.keyrings.Keyring;
17+
18+
import java.util.function.Consumer;
1519

1620
import static java.util.Objects.requireNonNull;
1721
import static org.apache.commons.lang3.Validate.isTrue;
@@ -65,5 +69,10 @@ public T keyring(Keyring keyring) {
6569
}
6670

6771
abstract T getThis();
72+
73+
T applyMutation(Consumer<T> mutator) {
74+
mutator.accept(getThis());
75+
return getThis();
76+
}
6877
}
6978
}

src/test/java/com/amazonaws/encryptionsdk/AwsCryptoTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ private void doEncryptDecryptWithKeyring(final CryptoAlgorithm cryptoAlg, final
101101
encryptionClient_.setEncryptionAlgorithm(cryptoAlg);
102102
encryptionClient_.setEncryptionFrameSize(frameSize);
103103

104-
final byte[] cipherText = encryptionClient_.encrypt(EncryptRequest.builder()
104+
final byte[] cipherText = encryptionClient_.encrypt(request -> request
105105
.keyring(keyring)
106106
.encryptionContext(encryptionContext)
107-
.plaintext(plaintextBytes).build()).getResult();
108-
final byte[] decryptedText = encryptionClient_.decrypt(DecryptRequest.builder()
107+
.plaintext(plaintextBytes)).getResult();
108+
final byte[] decryptedText = encryptionClient_.decrypt(request -> request
109109
.keyring(keyring)
110-
.ciphertext(cipherText).build()).getResult();
110+
.ciphertext(cipherText)).getResult();
111111

112112
assertArrayEquals("Bad encrypt/decrypt for " + cryptoAlg, plaintextBytes, decryptedText);
113113
}
@@ -657,7 +657,6 @@ public void assertNullValidation() throws Exception {
657657
.cryptoMaterialsManager(cmm)
658658
.plaintextSize(42).build()
659659
);
660-
661660
assertNullChecks(encryptionClient_, "encryptData",
662661
MasterKeyProvider.class, provider,
663662
byte[].class, buf,

0 commit comments

Comments
 (0)