3
3
"""
4
4
This example shows how to use the streaming encrypt and decrypt APIs when working with files.
5
5
6
- For the purposes of this example, we demonstrate using AWS KMS,
6
+ This example uses an AWS KMS CMK ,
7
7
but you can use other key management options with the AWS Encryption SDK.
8
8
Look in the ``keyring`` and ``master_key_provider`` directories
9
9
for examples that demonstrate how to use other key management configurations.
@@ -21,7 +21,7 @@ def run(aws_kms_cmk, source_plaintext_filename):
21
21
:param str aws_kms_cmk: AWS KMS CMK ARN to use to protect data keys
22
22
:param str source_plaintext_filename: Path to plaintext file to encrypt
23
23
"""
24
- # We assume that you can also write in the directory containing the plaintext file,
24
+ # We assume that you can also write to the directory containing the plaintext file,
25
25
# so that is where we will put all of the results.
26
26
ciphertext_filename = source_plaintext_filename + ".encrypted"
27
27
decrypted_filename = ciphertext_filename + ".decrypted"
@@ -35,13 +35,13 @@ def run(aws_kms_cmk, source_plaintext_filename):
35
35
"the data you are handling" : "is what you think it is" ,
36
36
}
37
37
38
- # Create the keyring that determines how your keys are protected.
38
+ # Create the keyring that determines how your data keys are protected.
39
39
keyring = KmsKeyring (generator_key_id = aws_kms_cmk )
40
40
41
41
# Open the files you want to work with.
42
42
with open (source_plaintext_filename , "rb" ) as plaintext , open (ciphertext_filename , "wb" ) as ciphertext :
43
- # The streaming API provides you with a context manager
44
- # that you can read from similar to how you would read from a file.
43
+ # The streaming API provides a context manager.
44
+ # You can read from it just as you read from a file.
45
45
with aws_encryption_sdk .stream (
46
46
mode = "encrypt" , source = plaintext , encryption_context = encryption_context , keyring = keyring
47
47
) as encryptor :
@@ -63,17 +63,17 @@ def run(aws_kms_cmk, source_plaintext_filename):
63
63
# One benefit of using the streaming API is that
64
64
# we can check the encryption context in the header before we start decrypting.
65
65
#
66
- # Verify that the encryption context used in the decrypt operation matches what you expect .
66
+ # Verify that the encryption context used in the decrypt operation includes the encryption context that you specified when encrypting .
67
67
# The AWS Encryption SDK can add pairs, so don't require an exact match.
68
68
#
69
69
# In production, always use a meaningful encryption context.
70
70
assert set (encryption_context .items ()) <= set (decryptor .header .encryption_context .items ())
71
71
72
- # Now that we are confident that the message is what we think it should be ,
72
+ # Now that we are more confident that we will decrypt the right message ,
73
73
# we can start decrypting.
74
74
for chunk in decryptor :
75
75
decrypted .write (chunk )
76
76
77
- # Verify that the "cycled" (encrypted then decrypted) plaintext
77
+ # Verify that the decrypted plaintext
78
78
# is identical to the original plaintext.
79
79
assert filecmp .cmp (source_plaintext_filename , decrypted_filename )
0 commit comments