def base64_encode(string):
Removes any `=` used as padding from the encoded string.
encoded = base64.b64encode(string.encode())
encoded = encoded.rstrip(b'=')
def generate_url(candidate):
{ "name": "text_candidate", "text": candidate }
# ensure no spaces in json output
encoded = json.dumps(modifications, separators=(',', ':'))
encoded = base64_encode(encoded).decode()
parameters = "{user_id}+{base_id}+{modifications}".format(user_id=user_id, base_id=base_id, modifications=encoded)
signature = hmac.new(secret_key.encode(), parameters.encode(), hashlib.sha256).hexdigest()
url = "https://images.usestencil.com/signed-images/{user_id}/{base_id}.png?modifications={modifications}&s={signature}".format(
def lambda_handler(event, context):
# we read the name from the query string
# i.e. https://xxx.amazonaws.com/default/secure_signed_image?name=David
candidate = event["queryStringParameters"]["name"]
url = generate_url(candidate)