|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Example showing basic encryption and decryption of streaming data in memory using one KMS CMK.""" |
| 14 | +import filecmp |
| 15 | + |
| 16 | +import aws_encryption_sdk |
| 17 | + |
| 18 | + |
| 19 | +def encrypt_decrypt_stream(key_arn, source_plaintext_filename, botocore_session=None): |
| 20 | + """Encrypts and then decrypts streaming data under one KMS customer master key (CMK). |
| 21 | +
|
| 22 | + :param str key_arn: Amazon Resource Name (ARN) of the KMS CMK |
| 23 | + :param str source_plaintext_filename: Filename of file to encrypt |
| 24 | + :param botocore_session: existing botocore session instance |
| 25 | + :type botocore_session: botocore.session.Session |
| 26 | + """ |
| 27 | + kwargs = dict() |
| 28 | + |
| 29 | + kwargs["key_ids"] = [key_arn] |
| 30 | + |
| 31 | + if botocore_session is not None: |
| 32 | + kwargs["botocore_session"] = botocore_session |
| 33 | + |
| 34 | + # Create master key provider using the ARN of the key and the session (botocore_session) |
| 35 | + kms_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(**kwargs) |
| 36 | + |
| 37 | + ciphertext_filename = source_plaintext_filename + ".encrypted" |
| 38 | + decrypted_text_filename = source_plaintext_filename + ".decrypted" |
| 39 | + |
| 40 | + # Encrypt the plaintext using the AWS Encryption SDK. |
| 41 | + with open(source_plaintext_filename, "rb") as plaintext, open(ciphertext_filename, "wb") as ciphertext: |
| 42 | + with aws_encryption_sdk.stream(source=plaintext, mode="e", key_provider=kms_key_provider) as encryptor: |
| 43 | + for chunk in encryptor: |
| 44 | + ciphertext.write(chunk) |
| 45 | + |
| 46 | + # Decrypt the encrypted message using the AWS Encryption SDK. |
| 47 | + with open(ciphertext_filename, "rb") as ciphertext, open(decrypted_text_filename, "wb") as plaintext: |
| 48 | + with aws_encryption_sdk.stream(source=ciphertext, mode="d", key_provider=kms_key_provider) as decryptor: |
| 49 | + for chunk in decryptor: |
| 50 | + plaintext.write(chunk) |
| 51 | + |
| 52 | + # Check if the original message and the decrypted message are the same |
| 53 | + assert filecmp.cmp(source_plaintext_filename, decrypted_text_filename) |
| 54 | + |
| 55 | + # Check if the headers of the encrypted message and decrypted message match |
| 56 | + assert all( |
| 57 | + pair in encryptor.header.encryption_context.items() for pair in decryptor.header.encryption_context.items() |
| 58 | + ) |
| 59 | + return ciphertext_filename, decrypted_text_filename |
0 commit comments