Skip to content

Commit 69411d6

Browse files
committed
MPL Examples
1 parent 8d51d92 commit 69411d6

File tree

18 files changed

+1270
-198
lines changed

18 files changed

+1270
-198
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<dependency>
7777
<groupId>software.amazon.cryptography</groupId>
7878
<artifactId>aws-cryptographic-material-providers</artifactId>
79-
<version>unspecified</version>
79+
<version>1.9.0-rc-sign-last-modified-time</version>
8080
</dependency>
8181

8282
<dependency>

src/examples/java/com/amazonaws/crypto/examples/keyrings/BasicEncryptionKeyringExample.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import java.nio.charset.StandardCharsets;
1515
import java.util.Arrays;
16-
import java.util.Collections;
16+
import java.util.HashMap;
1717
import java.util.Map;
1818

1919
/**
@@ -66,22 +66,36 @@ public static void encryptAndDecryptWithKeyring(final String keyArn) {
6666
// to protect integrity. This sample uses placeholder values.
6767
// For more information see:
6868
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
69-
final Map<String, String> encryptionContext =
70-
Collections.singletonMap("ExampleContextKey", "ExampleContextValue");
69+
final Map<String, String> encryptionContext = new HashMap<>();
70+
encryptionContext.put("encryption", "context");
71+
encryptionContext.put("is not", "secret");
72+
encryptionContext.put("but adds", "useful metadata");
73+
encryptionContext.put("that can help you", "be confident that");
74+
encryptionContext.put("the data you are handling", "is what you think it is");
75+
encryptionContext.put("𐀂","𐀂");
7176

7277
// 4. Encrypt the data
7378
final CryptoResult<byte[], ?> encryptResult =
7479
crypto.encryptData(kmsKeyring, EXAMPLE_DATA, encryptionContext);
7580
final byte[] ciphertext = encryptResult.getResult();
7681

82+
83+
final Map<String, String> encryptionContextOnDecrypt = new HashMap<>();
84+
encryptionContextOnDecrypt.put("encryption fails", "context fails");
85+
encryptionContextOnDecrypt.put("is not fails", "secret fails");
86+
encryptionContextOnDecrypt.put("but adds fails", "useful metadata fails");
87+
encryptionContextOnDecrypt.put("that can help you fails", "be confident that fails");
88+
encryptionContextOnDecrypt.put("the data you are handling", "is what you think it is");
89+
encryptionContextOnDecrypt.put("𐀂","𐀂");
90+
7791
// 5. Decrypt the data
7892
final CryptoResult<byte[], ?> decryptResult =
7993
crypto.decryptData(
8094
kmsKeyring,
8195
ciphertext,
8296
// Verify that the encryption context in the result contains the
8397
// encryption context supplied to the encryptData method
84-
encryptionContext);
98+
encryptionContextOnDecrypt);
8599

86100
// 6. Verify that the decrypted plaintext matches the original plaintext
87101
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import software.amazon.cryptography.materialproviders.model.CreateRawAesKeyringInput;
1313
import software.amazon.cryptography.materialproviders.model.MaterialProvidersConfig;
1414

15+
import javax.crypto.KeyGenerator;
16+
import javax.crypto.SecretKey;
1517
import java.nio.ByteBuffer;
1618
import java.nio.charset.StandardCharsets;
1719
import java.security.NoSuchAlgorithmException;
1820
import java.security.SecureRandom;
1921
import java.util.Arrays;
20-
import java.util.Collections;
22+
import java.util.HashMap;
2123
import java.util.Map;
22-
import javax.crypto.KeyGenerator;
23-
import javax.crypto.SecretKey;
2424

2525
/**
2626
* Encrypts and then decrypts data using an Raw Aes Keyring.
@@ -74,22 +74,35 @@ public static void encryptAndDecryptWithKeyring(final ByteBuffer aesKeyBytes) {
7474
// to protect integrity. This sample uses placeholder values.
7575
// For more information see:
7676
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
77-
final Map<String, String> encryptionContext =
78-
Collections.singletonMap("ExampleContextKey", "ExampleContextValue");
77+
final Map<String, String> encryptionContext = new HashMap<>();
78+
encryptionContext.put("encryption", "context");
79+
encryptionContext.put("is not", "secret");
80+
encryptionContext.put("but adds", "useful metadata");
81+
82+
encryptionContext.put("that can help you", "be confident that");
83+
encryptionContext.put("the data you are handling", "is what you think it is");
84+
encryptionContext.put("𐀂","𐀂");
7985

8086
// 4. Encrypt the data
8187
final CryptoResult<byte[], ?> encryptResult =
8288
crypto.encryptData(rawAesKeyring, EXAMPLE_DATA, encryptionContext);
8389
final byte[] ciphertext = encryptResult.getResult();
8490

91+
final Map<String, String> encryptionContextOutput = new HashMap<>();
92+
encryptionContextOutput.put("encryption", "context");
93+
encryptionContextOutput.put("is not", "secret");
94+
encryptionContextOutput.put("but adds", "useful metadata");
95+
encryptionContextOutput.put("that can help you", "be confident that");
96+
encryptionContextOutput.put("the data you are handling", "is what you think it is");
97+
encryptionContextOutput.put("𐀂","𐀂");
8598
// 5. Decrypt the data
8699
final CryptoResult<byte[], ?> decryptResult =
87100
crypto.decryptData(
88101
rawAesKeyring,
89102
ciphertext,
90103
// Verify that the encryption context in the result contains the
91104
// encryption context supplied to the encryptData method
92-
encryptionContext);
105+
encryptionContextOutput);
93106

94107
// 6. Verify that the decrypted plaintext matches the original plaintext
95108
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);

src/examples/java/com/amazonaws/crypto/examples/keyrings/RequiredEncryptionContextCMMExample.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,25 @@ public static void encryptAndDecryptWithKeyring(final String keyArn) {
5656
// For more information see:
5757
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
5858
final Map<String, String> encryptionContext = new HashMap<>();
59-
encryptionContext.put("key1", "value1");
60-
encryptionContext.put("key2", "value2");
61-
encryptionContext.put("requiredKey1", "requiredValue1");
62-
encryptionContext.put("requiredKey2", "requiredValue2");
59+
encryptionContext.put("encryption", "context");
60+
encryptionContext.put("is not", "secret");
61+
encryptionContext.put("but adds", "useful metadata");
62+
encryptionContext.put("that can help you", "be confident that");
63+
encryptionContext.put("the data you are handling", "is what you think it is");
64+
encryptionContext.put("𐀂","𐀂");
6365

6466
// 3. Create list of required encryption context keys.
6567
// This is a list of keys that must be present in the encryption context.
6668
final List<String> requiredEncryptionContextKeys =
67-
Arrays.asList("requiredKey1", "requiredKey2");
69+
Arrays.asList("requiredKey1", "𐀂");
6870

6971
// 4. Create the AWS KMS keyring.
7072
final MaterialProviders materialProviders =
7173
MaterialProviders.builder()
7274
.MaterialProvidersConfig(MaterialProvidersConfig.builder().build())
7375
.build();
7476
final CreateAwsKmsKeyringInput keyringInput =
75-
CreateAwsKmsKeyringInput.builder().kmsKeyId(keyArn).kmsClient(KmsClient.create()).build();
77+
CreateAwsKmsKeyringInput.builder().kmsKeyId("arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f").kmsClient(KmsClient.create()).build();
7678
final IKeyring kmsKeyring = materialProviders.CreateAwsKmsKeyring(keyringInput);
7779

7880
// 5. Create the required encryption context CMM.
@@ -90,15 +92,17 @@ public static void encryptAndDecryptWithKeyring(final String keyArn) {
9092
// 6. Encrypt the data
9193
final CryptoResult<byte[], ?> encryptResult =
9294
crypto.encryptData(requiredCMM, EXAMPLE_DATA, encryptionContext);
93-
final byte[] ciphertext = encryptResult.getResult();
95+
final byte[] ciphertext = new byte[] {
96+
2, 5, 120, 76, -95, 111, 114, 108, 90, -2, -80, 72, -78, -2, -102, 62, 3, 83, -70, 91, -92, 94, 2, 3, 76, -9, 44, 122, 127, -97, -21, -100, -53, -88, 67, 1, 5, 0, 7, 0, 21, 97, 119, 115, 45, 99, 114, 121, 112, 116, 111, 45, 112, 117, 98, 108, 105, 99, 45, 107, 101, 121, 0, 68, 65, 53, 71, 65, 73, 75, 103, 108, 77, 56, 43, 97, 74, 88, 109, 88, 68, 88, 76, 90, 51, 103, 101, 75, 102, 118, 70, 66, 49, 116, 78, 74, 65, 99, 75, 98, 106, 68, 120, 67, 106, 75, 48, 55, 77, 99, 122, 54, 90, 108, 109, 89, 54, 51, 80, 49, 67, 84, 111, 77, 49, 57, 114, 81, 118, 65, 61, 61, 0, 8, 98, 117, 116, 32, 97, 100, 100, 115, 0, 15, 117, 115, 101, 102, 117, 108, 32, 109, 101, 116, 97, 100, 97, 116, 97, 0, 10, 101, 110, 99, 114, 121, 112, 116, 105, 111, 110, 0, 7, 99, 111, 110, 116, 101, 120, 116, 0, 6, 105, 115, 32, 110, 111, 116, 0, 6, 115, 101, 99, 114, 101, 116, 0, 17, 116, 104, 97, 116, 32, 99, 97, 110, 32, 104, 101, 108, 112, 32, 121, 111, 117, 0, 17, 98, 101, 32, 99, 111, 110, 102, 105, 100, 101, 110, 116, 32, 116, 104, 97, 116, 0, 25, 116, 104, 101, 32, 100, 97, 116, 97, 32, 121, 111, 117, 32, 97, 114, 101, 32, 104, 97, 110, 100, 108, 105, 110, 103, 0, 23, 105, 115, 32, 119, 104, 97, 116, 32, 121, 111, 117, 32, 116, 104, 105, 110, 107, 32, 105, 116, 32, 105, 115, 0, 4, -16, -112, -128, -126, 0, 4, -16, -112, -128, -126, 0, 1, 0, 7, 97, 119, 115, 45, 107, 109, 115, 0, 75, 97, 114, 110, 58, 97, 119, 115, 58, 107, 109, 115, 58, 117, 115, 45, 119, 101, 115, 116, 45, 50, 58, 54, 53, 56, 57, 53, 54, 54, 48, 48, 56, 51, 51, 58, 107, 101, 121, 47, 98, 51, 53, 51, 55, 101, 102, 49, 45, 100, 56, 100, 99, 45, 52, 55, 56, 48, 45, 57, 102, 53, 97, 45, 53, 53, 55, 55, 54, 99, 98, 98, 50, 102, 55, 102, 0, -89, 1, 1, 1, 0, 120, 64, -13, -116, 39, 94, 49, 9, 116, 22, -63, 7, 41, 81, 80, 87, 25, 100, -83, -93, -17, 28, 33, -23, 76, -117, -96, -67, -68, -99, 15, -76, 20, 0, 0, 0, 126, 48, 124, 6, 9, 42, -122, 72, -122, -9, 13, 1, 7, 6, -96, 111, 48, 109, 2, 1, 0, 48, 104, 6, 9, 42, -122, 72, -122, -9, 13, 1, 7, 1, 48, 30, 6, 9, 96, -122, 72, 1, 101, 3, 4, 1, 46, 48, 17, 4, 12, 27, -5, -44, -10, -81, -117, -24, 15, 70, 65, 72, 49, 2, 1, 16, -128, 59, -63, -20, -90, -89, -91, -25, -42, 110, -66, 76, -124, -66, 8, -29, 70, -24, 73, 59, -36, 74, 81, -102, -88, -5, 109, 115, -35, 22, 126, -31, 51, 20, -39, 122, 27, 21, 21, -116, 70, -63, -31, -86, -78, 12, -74, 85, -98, -120, 108, -107, -67, -19, 15, 124, 81, 54, 126, -32, -110, 2, 0, 0, 16, 0, -118, -67, 31, -114, 77, -114, 47, -89, 103, -69, 125, -91, 108, 107, 89, -46, 78, -11, -59, -58, 22, 83, -67, 29, 110, -110, 3, 25, -7, 73, -29, -127, 100, -86, -75, 36, -56, 40, -93, 89, 117, 40, 126, 63, 89, -109, -108, -89, -1, -1, -1, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 47, -31, -63, 46, 60, -63, 113, -88, -42, 13, 96, 122, -81, 58, 109, -77, 47, 15, 88, -25, 0, 103, 48, 101, 2, 49, 0, -102, 60, -75, -63, 49, -87, -93, -88, 88, 121, 62, -59, 84, -6, -70, -48, 66, -37, -21, -81, -38, 10, 69, -87, 1, 77, -58, -70, 21, 32, 46, 8, 18, 110, -58, -110, 1, -48, -66, 48, -15, -46, -40, -57, 46, -91, 92, -99, 2, 48, 55, -24, 94, -74, -122, 54, -46, 72, -79, -50, 54, 83, 51, 26, 82, 115, -69, -52, 77, 88, 64, 90, 41, -16, -114, -103, -39, -107, -112, -63, 70, -117, 113, 38, 29, -97, -31, 79, -99, -51, 85, -45, -94, -38, -55, -111, -109, 110
97+
};
9498

9599
// 7. Reproduce the encryption context.
96100
// The reproduced encryption context MUST contain a value for
97101
// every key in the configured required encryption context keys during encryption with
98102
// Required Encryption Context CMM.
99103
final Map<String, String> reproducedEncryptionContext = new HashMap<>();
100104
reproducedEncryptionContext.put("requiredKey1", "requiredValue1");
101-
reproducedEncryptionContext.put("requiredKey2", "requiredValue2");
105+
reproducedEncryptionContext.put("𐀂","𐀂");
102106

103107
// 8. Decrypt the data
104108
final CryptoResult<byte[], ?> decryptResult =

0 commit comments

Comments
 (0)