-
Notifications
You must be signed in to change notification settings - Fork 122
chore(docs): update README get started example #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -75,6 +75,8 @@ The following code sample demonstrates how to get started: | |||||
// You provide the KMS key ARN and plaintext string as arguments. | ||||||
package com.amazonaws.crypto.examples; | ||||||
|
||||||
import java.nio.charset.StandardCharsets; | ||||||
import java.util.Arrays; | ||||||
import java.util.Collections; | ||||||
import java.util.Map; | ||||||
|
||||||
|
@@ -97,20 +99,22 @@ public class StringExample { | |||||
|
||||||
// Set up the master key provider | ||||||
final KmsMasterKeyProvider prov = KmsMasterKeyProvider.builder().buildStrict(keyArn); | ||||||
|
||||||
// Set up encryption context | ||||||
final Map<String, String> context = Collections.singletonMap("ExampleContextKey", "ExampleContextValue"); | ||||||
|
||||||
// Encrypt the data | ||||||
// | ||||||
// NOTE: Encrypted data should have associated encryption context | ||||||
// to protect integrity. For this example, just use a placeholder | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We don't want to tell users to use placeholder values. |
||||||
// value. For more information about encryption context, see | ||||||
// https://amzn.to/1nSbe9X (blogs.aws.amazon.com) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
final Map<String, String> context = Collections.singletonMap("Example", "String"); | ||||||
|
||||||
final String ciphertext = crypto.encryptString(prov, data, context).getResult(); | ||||||
System.out.println("Ciphertext: " + ciphertext); | ||||||
final CryptoResult<byte[], KmsMasterKey> encryptResult = crypto.encryptData(prov, data.getBytes(StandardCharsets.UTF_8), context); | ||||||
final byte[] ciphertext = encryptResult.getResult(); | ||||||
System.out.println("Ciphertext: " + Arrays.toString(ciphertext)); | ||||||
|
||||||
// Decrypt the data | ||||||
final CryptoResult<String, KmsMasterKey> decryptResult = crypto.decryptString(prov, ciphertext); | ||||||
final CryptoResult<byte[], KmsMasterKey> decryptResult = crypto.decryptData(prov, ciphertext); | ||||||
// Check the encryption context (and ideally the master key) to | ||||||
// ensure this is the expected ciphertext | ||||||
if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) { | ||||||
|
@@ -119,14 +123,15 @@ public class StringExample { | |||||
|
||||||
// The SDK may add information to the encryption context, so check to | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// ensure all of the values are present | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for (final Map.Entry<String, String> e : context.entrySet()) { | ||||||
if (!e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey()))) { | ||||||
if (!context.entrySet().stream | ||||||
.allMatch( e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) { | ||||||
throw new IllegalStateException("Wrong Encryption Context!"); | ||||||
} | ||||||
} | ||||||
|
||||||
assert Arrays.equals(decryptResult.getResult(), data.getBytes(StandardCharsets.UTF_8)); | ||||||
|
||||||
// The data is correct, so output it. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
System.out.println("Decrypted: " + decryptResult.getResult()); | ||||||
System.out.println("Decrypted: " + Arrays.toString(decryptResult.getResult())); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should print the decrypted data as a string since that's how it arrived:
Suggested change
|
||||||
} | ||||||
} | ||||||
``` | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.