2323import com .amazonaws .encryptionsdk .CryptoResult ;
2424import com .amazonaws .encryptionsdk .TestUtils ;
2525import com .amazonaws .encryptionsdk .model .CipherBlockHeaders ;
26+ import java .io .ByteArrayInputStream ;
27+ import java .io .ByteArrayOutputStream ;
28+ import java .nio .ByteBuffer ;
29+ import java .nio .charset .StandardCharsets ;
30+ import java .security .SecureRandom ;
31+ import java .util .Collections ;
2632import javax .crypto .SecretKey ;
2733import javax .crypto .spec .SecretKeySpec ;
2834import org .junit .Before ;
3440import software .amazon .cryptography .materialproviders .model .CreateRawAesKeyringInput ;
3541import software .amazon .cryptography .materialproviders .model .MaterialProvidersConfig ;
3642
37- import java .io .ByteArrayInputStream ;
38- import java .io .ByteArrayOutputStream ;
39- import java .nio .ByteBuffer ;
40- import java .nio .charset .StandardCharsets ;
41- import java .security .SecureRandom ;
42- import java .util .Collections ;
43-
4443public class BlockEncryptionHandlerTest {
4544 private final CryptoAlgorithm cryptoAlgorithm_ = TestUtils .DEFAULT_TEST_CRYPTO_ALG ;
4645 private final byte [] messageId_ =
@@ -86,9 +85,9 @@ public void correctIVGenerated() throws Exception {
8685 }
8786
8887 /**
89- * This isn't a unit test, but it reproduces a bug found in the FrameEncryptionHandler where the stream
90- * would be truncated when the offset is >0.
91- * For robustness' sake, the same test is included against the BlockEncryptionHandler.
88+ * This isn't a unit test, but it reproduces a bug found in the FrameEncryptionHandler where the
89+ * stream would be truncated when the offset is >0. For the sake of robustness, the same test is
90+ * included against the BlockEncryptionHandler.
9291 *
9392 * @throws Exception
9493 */
@@ -100,21 +99,19 @@ public void testStreamTruncation() throws Exception {
10099 rnd .nextBytes (rawKey );
101100 SecretKeySpec cryptoKey = new SecretKeySpec (rawKey , "AES" );
102101 MaterialProviders materialProviders =
103- MaterialProviders .builder ()
104- .MaterialProvidersConfig (MaterialProvidersConfig .builder ().build ())
105- .build ();
102+ MaterialProviders .builder ()
103+ .MaterialProvidersConfig (MaterialProvidersConfig .builder ().build ())
104+ .build ();
106105 CreateRawAesKeyringInput keyringInput =
107- CreateRawAesKeyringInput .builder ()
108- .wrappingKey (ByteBuffer .wrap (cryptoKey .getEncoded ()))
109- .keyNamespace ("Example" )
110- .keyName ("RandomKey" )
111- .wrappingAlg (AesWrappingAlg .ALG_AES128_GCM_IV12_TAG16 )
112- .build ();
106+ CreateRawAesKeyringInput .builder ()
107+ .wrappingKey (ByteBuffer .wrap (cryptoKey .getEncoded ()))
108+ .keyNamespace ("Example" )
109+ .keyName ("RandomKey" )
110+ .wrappingAlg (AesWrappingAlg .ALG_AES128_GCM_IV12_TAG16 )
111+ .build ();
113112 IKeyring keyring = materialProviders .CreateRawAesKeyring (keyringInput );
114113 // Use unframed (block) encryption
115- AwsCrypto crypto = AwsCrypto .builder ()
116- .withEncryptionFrameSize (0 )
117- .build ();
114+ AwsCrypto crypto = AwsCrypto .builder ().withEncryptionFrameSize (0 ).build ();
118115
119116 String testDataString = StringUtils .repeat ("Hello, World! " , 5_000 );
120117
@@ -125,22 +122,22 @@ public void testStreamTruncation() throws Exception {
125122 // copy some data, starting at the startOffset
126123 // so the first |startOffset| bytes are 0s
127124 System .arraycopy (
128- testDataString .getBytes (StandardCharsets .UTF_8 ),
129- 0 ,
130- inputDataWithOffset ,
131- startOffset ,
132- dataLength );
125+ testDataString .getBytes (StandardCharsets .UTF_8 ),
126+ 0 ,
127+ inputDataWithOffset ,
128+ startOffset ,
129+ dataLength );
133130 // decryptData (non-streaming) doesn't know about the offset
134131 // it will strip out the original 0s
135132 byte [] expectedOutput = new byte [10_000 - startOffset ];
136133 System .arraycopy (
137- testDataString .getBytes (StandardCharsets .UTF_8 ), 0 , expectedOutput , 0 , dataLength );
134+ testDataString .getBytes (StandardCharsets .UTF_8 ), 0 , expectedOutput , 0 , dataLength );
138135
139136 // Encrypt the data
140137 byte [] encryptedData ;
141138 try (ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
142139 try (CryptoOutputStream cryptoOutput =
143- crypto .createEncryptingStream (keyring , os , Collections .emptyMap ())) {
140+ crypto .createEncryptingStream (keyring , os , Collections .emptyMap ())) {
144141 cryptoOutput .write (inputDataWithOffset , startOffset , dataLength );
145142 }
146143 encryptedData = os .toByteArray ();
@@ -155,7 +152,7 @@ public void testStreamTruncation() throws Exception {
155152 int decryptedLength = 0 ;
156153 byte [] decryptedData = new byte [inputDataWithOffset .length ];
157154 try (ByteArrayInputStream is = new ByteArrayInputStream (encryptedData );
158- CryptoInputStream cryptoInput = crypto .createDecryptingStream (keyring , is )) {
155+ CryptoInputStream cryptoInput = crypto .createDecryptingStream (keyring , is )) {
159156 int offset = startOffset ;
160157 do {
161158 int bytesRead = cryptoInput .read (decryptedData , offset , decryptedData .length - offset );
0 commit comments