Skip to content

Commit 0bd2587

Browse files
Updating wording of some examples
1 parent 3351421 commit 0bd2587

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,15 @@ static void encryptAndDecrypt(final AwsKmsCmkId keyArn) {
7878
.keyring(keyring)
7979
.ciphertext(ciphertext).build());
8080

81-
// 6. Before verifying the plaintext, inspect the Keyring Trace to verify that the CMK used
82-
// to decrypt the encrypted data key was the CMK in the encryption keyring.
81+
// 6. The Keyring Trace may be inspected to verify which CMK was used for decryption.
8382
if(!decryptResult.getKeyringTrace().getEntries().get(0).getKeyName().equals(keyArn.toString())) {
8483
throw new IllegalStateException("Wrong key ID!");
8584
}
8685

87-
// 7. Also, verify that the encryption context in the result contains the
88-
// encryption context supplied to the encrypt method. Because the
89-
// SDK can add values to the encryption context, don't require that
90-
// the entire context matches.
91-
if (!encryptionContext.entrySet().stream()
92-
.allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) {
93-
throw new IllegalStateException("Wrong Encryption Context!");
94-
}
86+
// 7. Verify that the encryption context in the result contains the
87+
// data that we expect. The SDK can add values to the encryption context,
88+
// so there may be additional keys in the result context.
89+
assert decryptResult.getEncryptionContext().get("ExampleContextKey").equals("ExampleContextValue");
9590

9691
// 8. Verify that the decrypted plaintext matches the original plaintext
9792
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,12 @@ static void encryptAndDecrypt() {
7676
.keyring(keyring)
7777
.ciphertext(ciphertext).build());
7878

79-
// 7. Before verifying the plaintext, verify that the key that was used in the encryption
80-
// operation was the one used during the decryption operation.
81-
if (!decryptResult.getKeyringTrace().getEntries().get(0).getKeyName().equals("ExampleKeyName")) {
82-
throw new IllegalStateException("Wrong key ID!");
83-
}
79+
// 7. Verify that the encryption context in the result contains the
80+
// data that we expect. The SDK can add values to the encryption context,
81+
// so there may be additional keys in the result context.
82+
assert decryptResult.getEncryptionContext().get("ExampleContextKey").equals("ExampleContextValue");
8483

85-
// 8. Also, verify that the encryption context in the result contains the
86-
// encryption context supplied to the encrypt method. Because the
87-
// SDK can add values to the encryption context, don't require that
88-
// the entire context matches.
89-
if (!encryptionContext.entrySet().stream()
90-
.allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) {
91-
throw new IllegalStateException("Wrong Encryption Context!");
92-
}
93-
94-
// 9. Verify that the decrypted plaintext matches the original plaintext
84+
// 8. Verify that the decrypted plaintext matches the original plaintext
9585
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);
9686
}
9787

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ public static byte[] decrypt(byte[] ciphertext, KeyPair keyPair) {
4343
.keyring(keyring)
4444
.ciphertext(ciphertext).build());
4545

46-
// 4. Return the decrypted byte array result
46+
// 4. Verify that the encryption context in the result contains the
47+
// data that we expect. The SDK can add values to the encryption context,
48+
// so there may be additional keys in the result context.
49+
assert decryptResult.getEncryptionContext().get("ExampleContextKey").equals("ExampleContextValue");
50+
51+
// 5. Return the decrypted byte array result
4752
return decryptResult.getResult();
4853
}
4954
}

0 commit comments

Comments
 (0)