-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCipherAsyncRequestBody.java
More file actions
50 lines (42 loc) · 2.05 KB
/
CipherAsyncRequestBody.java
File metadata and controls
50 lines (42 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.encryption.s3.internal;
import org.reactivestreams.Subscriber;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.encryption.s3.S3EncryptionClientException;
import software.amazon.encryption.s3.materials.CryptographicMaterials;
import java.nio.ByteBuffer;
import java.util.Optional;
/**
* An AsyncRequestBody which encrypts and decrypts data as it passes through
* using a configured Cipher instance.
*/
public class CipherAsyncRequestBody implements AsyncRequestBody {
private final AsyncRequestBody wrappedAsyncRequestBody;
private final Long ciphertextLength;
private final CryptographicMaterials materials;
private final byte[] iv;
private final boolean isLastPart;
public CipherAsyncRequestBody(final AsyncRequestBody wrappedAsyncRequestBody, final Long ciphertextLength, final CryptographicMaterials materials, final byte[] iv, final boolean isLastPart) {
this.wrappedAsyncRequestBody = wrappedAsyncRequestBody;
this.ciphertextLength = ciphertextLength;
this.materials = materials;
this.iv = iv;
this.isLastPart = isLastPart;
}
public CipherAsyncRequestBody(final AsyncRequestBody wrappedAsyncRequestBody, final Long ciphertextLength, final CryptographicMaterials materials, final byte[] iv) {
// When no partType is specified, it's not multipart,
// so there's one part, which must be the last
this(wrappedAsyncRequestBody, ciphertextLength, materials, iv, true);
}
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
wrappedAsyncRequestBody.subscribe(new CipherSubscriber(subscriber,
contentLength().orElseThrow(() -> new S3EncryptionClientException("Unbounded streams are currently not supported.")),
materials, iv, isLastPart));
}
@Override
public Optional<Long> contentLength() {
return Optional.of(ciphertextLength);
}
}