import base64 import boto3 from fansifter_common.utils.functional import lazy_proxy from app.config import settings _client = lazy_proxy( lambda: boto3.client("kms", region_name=settings.aws_region_name), ) def encrypt(plaintext: str) -> str: response = _client.encrypt( KeyId=settings.kms_key_arn, Plaintext=plaintext.encode(), EncryptionContext={"field": "token"}, ) return base64.b64encode(response["CiphertextBlob"]).decode() def decrypt(ciphertext: str) -> str: response = _client.decrypt( CiphertextBlob=base64.b64decode(ciphertext), KeyId=settings.kms_key_arn, EncryptionContext={"field": "token"}, ) return response["Plaintext"].decode()