|
18 | 18 | import java.nio.charset.StandardCharsets;
|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.Map;
|
| 21 | +import java.util.function.Consumer; |
21 | 22 |
|
22 | 23 | import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
|
23 | 24 | import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
|
@@ -255,6 +256,21 @@ public long estimateCiphertextSize(final EstimateCiphertextSizeRequest request)
|
255 | 256 | return cryptoHandler.estimateOutputSize(request.plaintextSize());
|
256 | 257 | }
|
257 | 258 |
|
| 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 | + |
258 | 274 | /**
|
259 | 275 | * Returns an encrypted form of {@code plaintext} that has been protected with {@link DataKey}s
|
260 | 276 | * that are in turn protected by {@link MasterKey MasterKeys} provided by
|
@@ -355,6 +371,21 @@ public AwsCryptoResult<byte[]> encrypt(final EncryptRequest request) {
|
355 | 371 | cryptoHandler.getMasterKeys(), cryptoHandler.getHeaders());
|
356 | 372 | }
|
357 | 373 |
|
| 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 | + |
358 | 389 | /**
|
359 | 390 | * Calls {@link #encryptData(MasterKeyProvider, byte[], Map)} on the UTF-8 encoded bytes of
|
360 | 391 | * {@code plaintext} and base64 encodes the result.
|
@@ -525,6 +556,21 @@ public AwsCryptoResult<byte[]> decrypt(final DecryptRequest request) {
|
525 | 556 | cryptoHandler.getMasterKeys(), cryptoHandler.getHeaders());
|
526 | 557 | }
|
527 | 558 |
|
| 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 | + |
528 | 574 | /**
|
529 | 575 | * Base64 decodes the {@code ciphertext} prior to decryption and then treats the results as a
|
530 | 576 | * UTF-8 encoded string.
|
@@ -658,6 +704,21 @@ public AwsCryptoOutputStream createEncryptingOutputStream(final CreateEncrypting
|
658 | 704 | request.cryptoMaterialsManager(), request.encryptionContext()));
|
659 | 705 | }
|
660 | 706 |
|
| 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 | + |
661 | 722 | /**
|
662 | 723 | * Returns a {@link CryptoInputStream} which encrypts the data after reading it from the
|
663 | 724 | * underlying {@link InputStream}.
|
@@ -748,6 +809,21 @@ public AwsCryptoInputStream createEncryptingInputStream(final CreateEncryptingIn
|
748 | 809 | return new AwsCryptoInputStream(request.inputStream(), cryptoHandler);
|
749 | 810 | }
|
750 | 811 |
|
| 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 | + |
751 | 827 | /**
|
752 | 828 | * Returns a {@link CryptoOutputStream} which decrypts the data prior to passing it onto the
|
753 | 829 | * underlying {@link OutputStream}.
|
@@ -812,6 +888,22 @@ public AwsCryptoOutputStream createDecryptingOutputStream(final CreateDecrypting
|
812 | 888 | return new AwsCryptoOutputStream(request.outputStream(), cryptoHandler);
|
813 | 889 | }
|
814 | 890 |
|
| 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 | + |
815 | 907 | /**
|
816 | 908 | * Returns a {@link CryptoInputStream} which decrypts the data after reading it from the
|
817 | 909 | * underlying {@link InputStream}.
|
@@ -844,6 +936,21 @@ public AwsCryptoInputStream createDecryptingInputStream(final CreateDecryptingIn
|
844 | 936 | return new AwsCryptoInputStream(request.inputStream(), cryptoHandler);
|
845 | 937 | }
|
846 | 938 |
|
| 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 | + |
847 | 954 | private MessageCryptoHandler getEncryptingStreamHandler(
|
848 | 955 | CryptoMaterialsManager materialsManager, Map<String, String> encryptionContext
|
849 | 956 | ) {
|
|
0 commit comments