Skip to content

Commit 37d57b5

Browse files
chore: Add back removed CiphertextHeaders.deserialize method
1 parent 69e7914 commit 37d57b5

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -503,20 +503,37 @@ private int parseComplete(final byte[] b, final int off) throws ParseException {
503503
return 0;
504504
}
505505

506+
506507
/**
507508
* Deserialize the provided bytes starting at the specified offset to construct an instance of
508-
* this class.
509+
* this class. Uses the default value for maxEncryptedDataKeys, which results in no limit.
509510
*
510-
* <p>This method parses the provided bytes for the individual fields in this class. This methods
511+
* <p>This method parses the provided bytes for the individual fields in this class. This method
511512
* also supports partial parsing where not all the bytes required for parsing the fields
512513
* successfully are available.
513514
*
514515
* @param b the byte array to deserialize.
515516
* @param off the offset in the byte array to use for deserialization.
516-
* @param maxEncryptedDataKeys the maximum number of EDKs to deserialize; zero indicates no
517-
* maximum
518517
* @return the number of bytes consumed in deserialization.
519518
*/
519+
public int deserialize(final byte[] b, final int off) throws ParseException {
520+
return deserialize(b, off, NO_MAX_ENCRYPTED_DATA_KEYS);
521+
}
522+
523+
/**
524+
* Deserialize the provided bytes starting at the specified offset to construct an instance of
525+
* this class.
526+
*
527+
* <p>This method parses the provided bytes for the individual fields in this class. This method
528+
* also supports partial parsing where not all the bytes required for parsing the fields
529+
* successfully are available.
530+
*
531+
* @param b the byte array to deserialize.
532+
* @param off the offset in the byte array to use for deserialization.
533+
* @param maxEncryptedDataKeys the maximum number of EDKs to deserialize; zero indicates no
534+
* maximum
535+
* @return the number of bytes consumed in deserialization.
536+
*/
520537
public int deserialize(final byte[] b, final int off, int maxEncryptedDataKeys)
521538
throws ParseException {
522539
if (b == null) {
@@ -835,6 +852,16 @@ public void setSuiteData(byte[] suiteData) {
835852
suiteData_ = suiteData.clone();
836853
}
837854

855+
/**
856+
* Return max encrypted data keys
857+
* Package scope for unit testing.
858+
*
859+
* @return int
860+
*/
861+
int getMaxEncryptedDataKeys() {
862+
return maxEncryptedDataKeys_;
863+
}
864+
838865
private static class PartialParseException extends Exception {
839866
private static final long serialVersionUID = 1L;
840867
final int bytesParsed_;

src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class CiphertextHeadersTest {
5151

5252
@Test
5353
public void serializeDeserialize() {
54+
int maxEncryptedDataKeys = 42;
55+
5456
Map<String, String> encryptionContext = new HashMap<String, String>(1);
5557
encryptionContext.put("ENC", "CiphertextHeader Test");
5658

@@ -60,9 +62,28 @@ public void serializeDeserialize() {
6062
final byte[] headerBytes = ciphertextHeaders.toByteArray();
6163
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
6264
reconstructedHeaders.deserialize(
63-
headerBytes, 0, CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
65+
headerBytes, 0, maxEncryptedDataKeys);
66+
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();
67+
68+
assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), maxEncryptedDataKeys);
69+
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
70+
}
71+
}
72+
73+
@Test
74+
public void serializeDeserializeDefaultMaxEncryptedDataKeys() {
75+
Map<String, String> encryptionContext = new HashMap<String, String>(1);
76+
encryptionContext.put("ENC", "CiphertextHeader Test");
77+
78+
for (CryptoAlgorithm alg : testAlgs) {
79+
final CiphertextHeaders ciphertextHeaders = createCiphertextHeaders(encryptionContext, alg);
80+
81+
final byte[] headerBytes = ciphertextHeaders.toByteArray();
82+
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
83+
reconstructedHeaders.deserialize(headerBytes, 0);
6484
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();
6585

86+
assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
6687
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
6788
}
6889
}

0 commit comments

Comments
 (0)