Encrypts text by using the specified key. :param key_id: The ARN or ID of the key to use for encryption. :param text: The text to encrypt. :return: The encrypted version of the text.
(self, key_id: str, text: str)
| 36 | |
| 37 | # snippet-start:[python.example_code.kms.Encrypt] |
| 38 | def encrypt(self, key_id: str, text: str) -> bytes: |
| 39 | """ |
| 40 | Encrypts text by using the specified key. |
| 41 | |
| 42 | :param key_id: The ARN or ID of the key to use for encryption. |
| 43 | :param text: The text to encrypt. |
| 44 | :return: The encrypted version of the text. |
| 45 | """ |
| 46 | try: |
| 47 | response = self.kms_client.encrypt(KeyId=key_id, Plaintext=text.encode()) |
| 48 | print( |
| 49 | f"The string was encrypted with algorithm {response['EncryptionAlgorithm']}" |
| 50 | ) |
| 51 | return response["CiphertextBlob"] |
| 52 | except ClientError as err: |
| 53 | if err.response["Error"]["Code"] == "DisabledException": |
| 54 | logger.error( |
| 55 | "Could not encrypt because the key %s is disabled.", key_id |
| 56 | ) |
| 57 | else: |
| 58 | logger.error( |
| 59 | "Couldn't encrypt text. Here's why: %s", |
| 60 | err.response["Error"]["Message"], |
| 61 | ) |
| 62 | raise |
| 63 | |
| 64 | # snippet-end:[python.example_code.kms.Encrypt] |
| 65 |
no outgoing calls
no test coverage detected